You are on page 1of 1

Static method

 Static method can directly call another static method or variable


 Static method can call instance method, through an object reference.
So if an object reference is passed to it as a parameter or it will create an instance of the
class it can use that object to call the instance method like main method is static it can call
instance method by creating an object of the class to which instance method belongs.
 Static method can be overloaded but can’t be overridden
 In an interface we can’t define a static method or we can say a static method can’t be
abstract. (from java8 we can define static method in interface, but they should be default
methods of interface and they can’t be implemented by the classes that implements the
interface. As from java 8, we can defile concrete method in interface (default methods))

The reason is abstract method are defined so that it can be overridden by the subclasses or by
the classes that implements the interface. Since static method can’t be overridden, they can’t be
abstract

 Can interface have static block: - No. because static block is used to initialise the static
member of the class and in interface when we define variable they are in essence final
static and public, so we need to initialize them during creation. So there is no use of
static block then

Constructor

 Constructor can be private, protected, default and public


 Private constructor is used: -
o if object creation is forbidden. Like if a class contains only static element, we don’t
want to create an object of that class
o or object construction is private only like a singleton class. In that case we declared
constructor as private and use a static method which will call the private constructor
to create object
 a constructor can’t be static
 a constructor can’t be overridden and thus can’t be abstract.

You might also like