Java Fundamentals – Static Keyword

7 min read

Hey Guys!! In this post, we will be learning about Static keyword in Java. We will go through the behavior of entities when Static Variable is used with them. Also, we will follow up with some interview related questions for beginners till Advance level.

Static keyword can be used with method, variables and independently with code blocks which are called static blocks.

  • Static Method A static method belongs to a class or is present at the class level. Like any other non-static member, first we need to instantiate the class in order to access it, with static methods, it can be called with the instance of the class without creating an object. Also, static methods are common to every instance of the class.

public class StaticMethod {
    public static void main(String[] args) {
          /* You can see that we are calling this
           * method without creating any object. 
           */
        Demo.staticMethod();
    }
}

class Demo {
    static void staticMethod() {
        System.out.println("static method");
    }
}

  • Static variables Static variables are also called as Class variables. These variables remains same for all the class objects or in other words they are shared by all the objects of a class. Memory allocations for static variable happens only once when the class is loaded.
public class StaticVariableDemo {
    static int a = 5;
    static int b = 8;

    static void prettyPrint(){
        System.out.println("Value of a: " +a);
        System.out.println("Value of b: " +b);
    }
     
    public static void main(String[] args) {
        prettyPrint();
    }
}

Next example of static variable will help us understand that the value of the static variable is shared among all the objects of the class.

public class StaticVariableDemo2 {
    static int a = 5;
    static int b = 8;

    static void prettyPrint(){
        System.out.println("Value of a: " +a);
        System.out.println("Value of b: " +b);
    }

    public static void main(String[] args) {
        StaticVariableDemo2 obj1 = new StaticVariableDemo2();
        StaticVariableDemo2 obj2 = new StaticVariableDemo2();

        obj1.prettyPrint(); 
        obj2.prettyPrint(); // same value for obj1 and obj2

        obj1.a= 10;
        obj1.b = 14;
        obj1.prettyPrint(); 
        obj2.prettyPrint(); // value also changed for obj2

    }
}
  • Static Block Static block is used for initializing the static variables. This block gets executed when the class is loaded in the memory. A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.

Question) Next, we would be showing an example of sequence of execution of static, non-static and constructors within a context of inheritance. This question is often asked in interviews of JAVA core interviews and tends out to be bit tricky if concepts are not crystal clear.

class Base {
    static{
        System.out.println("Base Class static Called ");
    }
    {
        System.out.println("Base Class non static Called ");
    }
    Base() {
        System.out.println("Base Class Constructor Called ");
    }
}

class Derived extends Base {
    static{
        System.out.println("Derived Class static Called ");
    }
    {
        System.out.println("Derived Class non static Called ");
    }
    Derived() {
        System.out.println("Derived Class Constructor Called ");
    }
}

In the above code snippet, First the static methods of Base class and then derived class is called because static methods are executed first as soon as class is loaded into JVM. Next up will be Non-static blocks o犀利士 f super class and then the constructor of parent class because Non-static gets called just before the constructors are called. Then follows up with non-static and constructor of derived class. Response of above code can be seen below.

Base Class static Called 
Derived Class static Called 
Base Class non static Called 
Base Class Constructor Called 
Derived Class non static Called 
Derived Class Constructor Called 

Question) Can constructors be static in Java?

No, constructors cannot be static in Java because static methods are at class level and constructors are at object level.

Question) Can an Interface in Java has static methods.

Yes, onwards JAVA 8, interface can have default and static methods in it.

Question) Can we have multiple static blocks in a class?.

Yes, we can have multiple static blocks in a class, there order of execution is same as the order of declaration.

Question) Can we declare a static class?

Yes, we can but only an inner class can be declared as static.

Question) Can we overload and override static method?

We can overload but cannot override a static method.

Question) What are the advantages of using static variable?

Static variable helps us write memory efficient code as the memory is only allocated once for the variable.

I have tried to compile the questions related to the topic in text/code explanation. Hopefully, it will be helpful in some way.

Happy Learning!!