Java static keyword

Java static keyword play a very important role in Java. This keyword is used for managing the memory efficiently. It belongs to the class level rather than the instance level.
In Java programming through static keyword we can define the following:

Static Variable: static variable use the common property for all the objects and this variable get the memory once at class loading.

Lets see an example:

class StaticTest {
  int rollNo;
  String name;
  static String collegeName ="MIT";
  
  StaticTest(int rollNo, String name) {
    this.rollNo = rollNo;
    this.name = name;
  }
  
  void show() {
    System.out.println("Roll No: " + rollNo);
    System.out.println("Name: " + name);
    System.out.println("College Name: " +collegeName);
  }
  
  public static void main(String args[]) {
    StaticTest s1 = new StaticTest(1,"Ankit");
    StaticTest s2 = new StaticTest(2,"Arjun");
    
    s1.show();
    s2.show();
  }
}

Output:

Roll No: 1
Name: Ankit
College Name: MIT
Roll No: 2
Name: Arjun
College Name: MIT

As per above example all instance data members(for eg. rollno, name) will get memory each time when object is created. While all students have unique data member that is collegeName which is static and will get memory only once while loading the class. This is a better approach for memory management as college name is common for all the students and we no need to create individual memory for this data member. Hence Java provide the static feature.
Note: All instance variable stored in Heap while the classes and the data(for eg. static data) applying to classes(not include the instance data) are stored in Permanent Generation(PermGen) section of the Heap.

Static Method: static method can invoke without creating the instance of class. It can access static data member and change it.
Lets see an example:

class StaticTest { 
  int rollNo;
  String name;
  static String collegeName ="MIT";

  static void changeCollegeName() {
    collegeName = "GEU";
  }

  StaticTest(int rollNo, String name) {
    this.rollNo = rollNo;
    this.name = name;
  }

  void show() { 
    System.out.println("Roll No: " + rollNo);
    System.out.println("Name: " + name);
    System.out.println("College Name: " +collegeName);
  }

  public static void main(String args[]) {
    // This change will apply on all instance
    StaticTest.changeCollegeName();
    StaticTest s1 = new StaticTest(1,"Ankit");
    StaticTest s2 = new StaticTest(2,"Arjun");
    s1.show();
    s2.show();
  }
}

Output:

Roll No: 1
Name: Ankit
College Name: GEU
Roll No: 2
Name: Arjun
College Name: GEU

As per above example we are trying to change the common property(eg. College name) for all the students that’s why we are using static method here because static method invoke by the class not by the instance. The changes which will occur by static method will apply or see by all the instance of that class.

Static Block: static block executes always before main() method. It can also use to initialize static data members. It is also possible to create multiple static block in a program, which will execute in the same manner in which they have been written.
Lets see an example:

class StaticTest {
  // First static block
  static {
    System.out.println("First static block");
  }

  // Constructor
  public StaticTest() {
    System.out.println("Constructor");
  }

  // Second static block
  static {
    System.out.println("Second static block");
  }

  public static void main(String args[]) {
    // In main method
    System.out.println("In main method");

    // Create the object
    StaticTest s1 = new StaticTest();
  }
}
Output:

First static block
Second static block
In main method
Constructor

As per above program first all the static block will execute on the same manner as they have been written then main method will invoke by JVM. Inside the main method we are creating the object hence constructor will call whenever any object is created.

Static Nested Class: A static nested class is simply a class inside another class in Java. Also static modifier says that the nested class can access other static members. Here we don’t need to create the object of outer class because the nested class is static.
Lets see an example:

class StaticTest {
  static class InnerStaticClass {
    void display() {
      System.out.println("Inside nested static class");
    }
  }
  public static void main(String args[]) {
    StaticTest.InnerStaticClass st1 = new StaticTest.InnerStaticClass();
    st1.display();
  }
}

Output:

Inside nested static class