Static Keyword

You can use static keyword in three scenario :

  1. static variables
  2. static methods
  3. static blocks of code

static variables

  • static variable belongs to the class and not to the object(instance).
  • Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables
  • A single copy of static variable is to be shared by all instances of the class.
  • A static variable can be accessed directly by the class name and doesn't need any object.
  • Syntax :
        <class-name >.<variable-name>

Example of static variable
public class StaticVariableExample 
{
	static int Instances;
	StaticVariableExample()
	{
		Instances++;
	}
	public static void main(String[] args){
		StaticVariableExample sve1 = new StaticVariableExample();
		System.out.println("No. of instances for sve1 : " 
		+ sve1.Instances);
		System.out.println("");
		StaticVariableExample sve2 = new StaticVariableExample();
		System.out.println("No. of instances for sve1 : " 
		 + sve1.Instances);
		System.out.println("No. of instances for st2 : " 
		 + sve2.Instances);
		System.out.println("");
		StaticVariableExample sve3 = new StaticVariableExample();
		System.out.println("No. of instances for sve1 : "  
		+ sve1.Instances);
		System.out.println("No. of instances for sve2 : "  
		+ sve2.Instances);
		System.out.println("No. of instances for sve3 : "  
		+ sve3.Instances);
		System.out.println("");
		StaticVariableExample sve4 = new StaticVariableExample();
		System.out.println("No. of instances for sve1 : "  
		+ sve1.Instances);
		System.out.println("No. of instances for sve2 : "  
		+ sve2.Instances);
		System.out.println("No. of instances for sve3 : "  
		+ sve3.Instances);
		System.out.println("No. of instances for sve4 : "  
		+ sve4.Instances);
		System.out.println("");
		StaticVariableExample sve5 = new StaticVariableExample();
		System.out.println("No. of instances for sve1 : " 
		 + sve1.Instances);
		System.out.println("No. of instances for sve2 : "  
		+ sve2.Instances);
		System.out.println("No. of instances for sve3 : "  
		+ sve3.Instances);
		System.out.println("No. of instances for sve4 : "  
		+ sve4.Instances);
		System.out.println("No. of instances for sve5 : "  
		+ sve5.Instances);
	}
}
	
output :
No. of instances for sve1 : 1 No. of instances for sve1 : 2 No. of instances for st2 : 2 No. of instances for sve1 : 3 No. of instances for sve2 : 3 No. of instances for sve3 : 3 No. of instances for sve1 : 4 No. of instances for sve2 : 4 No. of instances for sve3 : 4 No. of instances for sve4 : 4 No. of instances for sve1 : 5 No. of instances for sve2 : 5 No. of instances for sve3 : 5 No. of instances for sve4 : 5 No. of instances for sve5 : 5

static method

  • static method belongs to the class and not to the object(instance).
  • A static method can call only other static methods and can not call a non-static method from it.
  • A static method can access only static data. It can not access non-static data (instance variables).
  • A static method can be accessed directly by the class name and doesn't need any object.
  • Syntax :
        <class-name >.<method-name>
  • A static method cannot refer to "this" or "super" keywords in anyway.
public class StaticMethodExample 
{
	static int i=0;
	static void getmethod() 
	{
		i++;
	    System.out.println("Value of i is" +i);
	}
}
class TestStatic 
{
	  public static void main(String args[]) 
	  {
		  StaticMethodExample.getmethod();
	  }
}
	
output :
Value of i is 1

NOTE : main method is static, since it must be be accessible for an application to run, before any instantiation takes place.

static block

  • The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the JVM
public class StaticBlockExample {
  static int[] values = new int[10];

  static {
    System.out.println("Initialization block Running");
    for (int i = 0; i < values.length; i++) {
      values[i] = (int) (10.0 * Math.random());
    }
  }

  void listValues() {
    for (int value : values) {
      System.out.println(value);
    }
  }

  public static void main(String[] args) {
    StaticBlockExample example = new StaticBlockExample();
    System.out.println("\nValue of First object:");
    example.listValues();

    example = new StaticBlockExample();
    System.out.println("\nValue of Second object:");
    example.listValues();
  }
}
	
output :
Initialization block Running Value of First object: 9 7 5 6 9 0 2 2 8 3 Value of Second object: 9 7 5 6 9 0 2 2 8 3