static keyword in Java

static keyword is used mainly for memory management. If we give static keyword, the compiler will take that as a common (global) property for all objects of that class.

static keyword can be used in

  • variables
  • methods
  • blocks
  • nested classes

Static variables

Variables that are declared with static keyword inside a class is said as static variable.

static keyword tells the compiler there exists only one copy of the variable.

static variables memory allocation does not depend on the object creation and the memory is allocated before the main method is called.

The scope of instance variable belongs to the class and does not belong to any specific object.

Example

class StaticVariableTest{

static int data=15;

public static void main(String args[]){

StaticVariableTest svt1=new StaticVariableTest(),svt2=new StaticVariableTest();

svt1.data=12;

System.out.println("Value of variable 'data' "+svt2.data);

}

}

Try In Editor

Output

Value of variable 'data' 12

Look out the above example, we initialized 'data' variable of svt1 to 12.

When we print 'data' variable of svt2 object, it prints the overwritten value.

This is static variable. i.e the scope belongs to class not to the specific object.

Since it is a common for all objects for class, we can access directly by its name without creating object for that class. Similar example is given under static methods.


Static methods

Methods declared as static is said as static methods.

static methods belongs to class and common to the objects of that class.

Since it is common and doesn't belong to a particular object, we don't need to create a object. We can access the static methods directly with the help of class name.

Example

class StaticMethodTest{

static int data;

static void assign(int i){

data=i;

System.out.println("Value of 'data' variable is "+data);

}

public static void main(String args[]){

StaticMethodTest.assign(5);

}

}

Try In Editor

Output

Value of 'data' variable is 5


We cannot directly access the instance variables inside a static method, only static variables and local variables can be accessed.

Example

class StaticMethodTest2{

int data; //instance variable

static void assign(int i){

data=i;

}

public static void main(String args[]){

StaticMethodTest2.assign(5);

}

}

Try In Editor

Output

error: non-static variable data cannot be referenced from a static context


We also cannot call a non-static method directly from a static method without the creation of object.

Example

class StaticMethodTest3{

int data; //instance variable

void assign(int i){

data=i;

}

public static void main(String args[]){

assign(5);

}

}

Try In Editor

Output

error: non-static method assign(int) cannot be referenced from a static context


Why main method is static?

static method can be called with class name without creating an object. We run a program by its class name in command prompt. When we run the program using class name, static main method inside the class will be called automatically without object creation. If static is not given we need to call the main method after creating an object. To call the main method at that time of run, we use static.


Static block

static block is the part which runs before the main method of a class.

It can be used to define a value for static variable.

Example

class StaticBlockTest{

static int data;

static{

data=25;

System.out.println("Value of 'data' variable is "+data);

}

public static void main(String args[]){

System.out.println("hello world");

}

}

Try In Editor

Output

Value of 'data' variable is 25

hello world