static Keyword in Java

  • You can apply static keyword to variables, methods, nested classes, nested interfaces and blocks of code.
  • It is not valid to define top-level classes, interfaces, and enum as static.
  • Static members belong to a class and not to instances.

Static Variable in Java

  • It is also known as a class variable.
  • Static variables belong to a class and you can access even if no instance of a class is created.
  • Static variables are shared by all the instances of the class. If any object makes a change to a static variable then all other objects see that change.
  • You can access static variable without creating an object of a class.
  • It can be used to count the number of instances of a class.
  • A static variable is created when the class is loaded into memory.

Syntax of creating static variable-

access-modifier static data-type variable;

Example

static int counter;

Syntax of accessing static variable-

class-name.staticVariable;
or
object.staticVariable;

Example of static variable

public class StaticExample
{
	static int counter;
	public StaticExample()
	{
		counter++;
	}
	public static void main(String args[])
	{
		System.out.println("No of objects = "+StaticExample.counter);
		StaticExample se = new StaticExample();
		System.out.println("No of objects = "+se.counter);
		StaticExample se2 = new StaticExample();
		se2.counter=5;
		System.out.println("No of objects = "+se.counter);
	}
}

Output

No of objects = 0
No of objects = 1
No of objects = 5

Static Method in Java

  • Like a static variable, a static method belongs to a class and not to a specific instance of the class.
  • Static methods are defined to access and manipulate static members of a class.
  • Similar to static variables, you can access static methods using the class name and object.
  • It cannot access nonstatic or instance members of the class.
  • Static methods are allowed to access only static members of the class.

Syntax

access-modifier static return-type methodName(parameters)
{
	//code to be written inside method
}

Example of static method

class Mathematics
{
	public static int add(int n1, int n2)
	{
		return n1 + n2;
	}
	public static int sub(int n1, int n2)
	{
		return n1 - n2;
	}
	public static void main(String args[])
	{
		System.out.println("Sum of 10 and 12 is "+Mathematics.add(10,12));
		System.out.println("Subtraction of 10 and 12 is "+Mathematics.sub(10,12));
	}
}

Output

Sum of 10 and 12 is 22
Subtraction of 10 and 12 is -2

Static Block

  • A block of code enclose in braces and prefixed by static keyword is called static initializer block.
  • You cannot initialize static variables in the constructor of a class, by the help of static initializer block you can initialize static variables.
  • A class can have more than one static initializer blocks. And they are executed in the order of their appearance.

Syntax

static{
	//code to initialize static variables
}

Example of static initializer block

public class Bonus
{
	private static int reward;
	static{
		reward = 10;
	}
	public static void main(String args[])
	{
		System.out.println("Reward Point = "+Bonus.reward);
	}
}

Output

Reward Point = 10

Difference between static method and instance method

static method instance method
A method that is declared static is called static method. A method that is not declared without static keyword is called instance method.
static method can be called by class name or an instance of the class. Instance method can be only be called by object of the class.
It cannot be access non-static members of the class. Both static and non-static members can be access by instance methods.