Static Keyword

Static keyword can be used with the variables and methods and with inner class. Anything declared as static is related to class and not an object.

Static Variable

Static variable is associated with a class. Only one instance of variable is present in the memory making it memory efficient. If a class object is created 10 times all the instances will be pointing to the same instance. The instance of the variable is created in a heap.

public class Counter {
 private static int count=0;
 private int nonStaticcount=0;

 public void incrementCounter() {
   count++;
   nonStaticcount++;
 }
 
 public static int getCount() {
  return count;
 }
 
 public int getNonStaticcount() {
  return nonStaticcount;
 }
 
 public static void main(String args[]) {
 
   Counter countObj1 = new Counter(); 
   Counter countObj2 = new Counter();
   countObj1.incrementCounter();
   countObj2.incrementCounter();
   System.out.println("Static count for Obj1: "+countObj1.getCount());
   System.out.println("NonStatic count for Obj1: "+countObj1.getNonStaticcount());
   System.out.println("Static count for Obj2: "+countObj2.getCount())
   System.out.println("NonStatic count for Obj2: "+countObj2.getNonStaticcount())
 }
Output
Static count for Obj1: 2
NonStatic count for Obj1: 1
Static count for Obj2: 2
NonStatic count for Obj2: 1

Static Method

A static method can be accessed without creating the objects. Just by using the Class name the method can be accessed. Static method can only access static variables and not local or global non-static variables. Static methods can be accessed via it’s class reference, and there is no need to create an instance of class. Though you can access using instance reference as well but it will have not any difference in comparison to access via class reference.

public class Test{
 public static void printMe() {
  System.out.println("Hello World");
 }
}

public class MainClass {
  public static void main(String args[]) {
    Test.printMe()
  }
 }
 
Output:
Hello World

Static Class

In java, you can have a static class as inner class. Just like other static members, nested classed belong with class scope so the inner static class can be accessed without having an object of outer clas

A static member class behaves much like an ordinary top-level class, except that it can access the static members of the class that contains it. The static nested class can be accessed as the other static members of the enclosing class without having an instance of the outer class. The static class can contain non-static and static members and methods.

public class InnerClass { 
  static class StaticInner { 
    static int i = 9; 
    int no = 6; 
    private void method() {} 
    public void method1() {} 
    static void method2() {} 
    final void method3() {} 
  } 
}

Static Block

Static blocks are portion of class initialization code, which are wrapped with static keyword. The static block is loaded when the class is loaded by the JVM for the 1st time only whereas init {} block is loaded every time class is loaded. Also first the static block is loaded then the init block.

public class LoadingBlocks {
  static{
    System.out.println("Inside static");
  }
  
  {
    System.out.println("Inside init");
  }
  public static void main(String args[]){
    new LoadingBlocks();
    new LoadingBlocks();
    new LoadingBlocks();
  }
}

Output:

Inside static
Inside init
Inside init
Inside init

Recommend Reading

  1. How java manages Memory?
  2. Why is it advised to use hashcode and equals together?
  3. Comparable and Comparator in java
  4. How to create Singleton class in Java?
  5. Difference between equals and ==?
  6. When to use abstract class over interface?
  7. Difference between final, finally and finalize
  8. Why is it recommended to use Immutable objects in Java