Top 14 Java Interview Questions on Static keyword

1. What is static keyword in Java?

Static is a Non Access Modifier. Static can be applied to variable, method, nested class and initialization blocks (static block).

2. What is a static variable ?

  • A Static variable gets memory allocated only once during the time of class loading.
  • All the instance of the class share the same copy of the variable, a static variable can be accessed directly by calling “<<ClassName>>.<<VariableName>>” without need to create instance for the class.
  • value of a static variable will be common for all instances

public class StaticVariableExample 
{
    static int a =10;
    public static void main(String args[]){
        StaticVariableExample s1 = new StaticVariableExample();
        StaticVariableExample s2 = new StaticVariableExample();
        System.out.println("s1.a value :"+s1.a);
        System.out.println("s2.a value :"+s2.a);
        //Change s1 a value alone
        s1.a=20;
        System.out.println("s1.a value :"+s1.a);
        System.out.println("s2.a value :"+s2.a);
    }
}

Output will be
s1.a value :10
s2.a value :10
s1.a value :20
s2.a value :20

  • Local variables cannot be assigned as static it will throw compile time error “illegal start of expression”, as the memory cannot be assigned during class load.

3. What is a static method ?

  • static method belongs to class rather than object. It can be called directly by using the classname “<<ClassName>>.<<MethodName>>”
  • A static method can access static varaibles directly and it cannot access non-static variables and can only call a static method directly and it cannot call a non-static method from it.
  • Only the main() method which is static will be called by the JVM automatically, Not all the static method will be called automatically.

4. Can a static block exist without a main() method ?

Yes. You can have static block alone in the class without a main method.

5. Can we Overload static methods in Java

Yes, you can overload a static method in Java. Read More..

6. Can we Override static methods in Java

No, you cannot override a static method in Java as there will not be any Run-time Polymorphism happening. Read More..

7. Why main() method is declared as static ?

If our main() method is not declared as static then the JVM has to create object first and call which causes the problem of having extra memory allocation.

8. What is a static block ?

  • A static block, is a block of code inside a Java class that will be executed when a class is first loaded in to the JVM. Mostly the static block will be used for initializing the variables.
  • Static block will be called only one while loading and it cannot have any return type, or any keywords (this or super).
class test
{
	int val;
	static{
      val = 100;
    }
}

9.  Can we have multiple static blocks in our code ?

Yes, we can have more than one static block in our code. It will be executed in the same order it is written.

10. What is a static class ?

  • In Java only nested classes are allowed to be declared as static, a top level class cannot be declared as static.
  • Even though static classes are nested inside a class, they doesn’t need the reference of the outer class they act like outer class only. Read More..

11. Can constructors be static in Java?

In general a static method means that “The Method belong to class and not to any particular object” but a constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static.

12. Why abstract method cannot be static in Java?

Suppose when you have a concrete method in a abstract class then that method can be static. Suppose we have a class like below

public class AbstractTest
{
    static void disp()
    {
        System.out.println("disp of static method");
    }
}

Then the disp() can be access by “AbstractTest.disp()”
However, for the same reason cannot be applied when you declare a static method to be abstract. Since static method can be called directly, making it abstract would make it possible to call an undefined method which is of no use, hence it is not allowed.

13. Can Interface in Java have static methods in it?

No, Interface cannot have static methods in it because all methods are implicitly abstract. This is why an interface cannot have a static method.

14. Can abstract class have static variable in it?

Yes, an abstract class can have static variables in it.

Leave a Reply

Your email address will not be published. Required fields are marked *