Java Static – Variable, Method, Block, Class and Import Statement

Static keyword in java can be applied on variables, methods, blocks, import and inner classes. In this tutorial, we will learn the effect of using static keyword in these places with examples.

Table of Contents

1. Static Variable
2. Static Method
3. Static Import Statement
4. Static Block
5. Static Class
6. Summary

1. Static Variable

To declare a variable static, use static keyword in variable declaration. static variable syntax is:

ACCESS_MODIFER static DATA_TYPE VARNAME;

For example, a public static variable of Integer type is declared in this way.

public static Integer staticVar;

The most important thing about static variables is that they belong to class level. What it means is that there can be only one copy of variable in runtime.

When you define a static variable in class definition, each instance of class will have access to that single copy. Separate instances of class will not have their own local copy, like they have for non-static variables.

public class JavaStaticExample 
{
	public static void main(String[] args) 
	{
		DataObject objOne = new DataObject();
		objOne.staticVar = 10;
		objOne.nonStaticVar = 20;
		
		DataObject objTwo = new DataObject();
		
		System.out.println(objTwo.staticVar);		//10
		System.out.println(objTwo.nonStaticVar);	//null
		
		DataObject.staticVar = 30;	//Direct Access
		
		System.out.println(objOne.staticVar);		//30
		System.out.println(objTwo.staticVar);		//30
	}
}

class DataObject {
	public static Integer staticVar;
	public Integer nonStaticVar;
}

Output:

10
null
30
30

Notice how we changed the value to 30, and both objects now see the updated value which is 30.

Another thing you should have noticed that how we are able to access static variable with its classname i.e. DataObject.staticVar. We don’t need to create any instance to access static variables. It clearly shows that static variables belong to class scope.

2. Static Method

To declare a static method, use static keyword in method declaration. Static method syntax is:

ACCESS_MODIFER static RETURN_TYPE METHOD_NAME;

For example, a public static variable of Integer type is declared in this way.

public static Integer staticVar;

public static Integer getStaticVar(){

	return staticVar;
}

Few things to remember.

  1. You can access only static variables inside static methods. If you try to access any non-static variable, the compiler error will be generated with message “Cannot make a static reference to the non-static field nonStaticVar“.
  2. 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.
  3. Static methods also belong to class level scope.
public class JavaStaticExample 
{
	public static void main(String[] args) 
	{
		DataObject.staticVar = 30;	//Direct Access
		
		Integer value1 = DataObject.getStaticVar();	//access with class reference

		DataObject objOne = new DataObject();
		Integer value2 = objOne.getStaticVar();		//access with instance reference
		
		System.out.println(value1);
		System.out.println(value2);
	}
}

class DataObject 
{
	public Integer nonStaticVar;
	public static Integer staticVar;	//static variable
	
	public static Integer getStaticVar(){
		return staticVar;
	}
}

Output:

30
30

3. Static Import Statement

The normal import declaration imports classes from packages, so that they can be used without package reference. Similarly the static import declaration imports static members from classes and allowing them to be used without class reference.

A static import statement also comes in two flavors: single-static import and static-import-on-demand. A single-static import declaration imports one static member from a type. A static-import-on-demand declaration imports all static members of a type.

//Single-static-import declaration:
 
import static <<package name>>.<<type name>>.<<static member name>>;
 
//Static-import-on-demand declaration:
 
import static <<package name>>.<<type name>>.*;

For example, System.out is

//Static import statement
import static java.lang.System.out;

public class JavaStaticExample 
{
	public static void main(String[] args) 
	{
		DataObject.staticVar = 30;	

		out.println(DataObject.staticVar); 	//Static import statement example
	}
}
class DataObject 
{
	public static Integer staticVar;	//static variable
}

Output:

30

Read More: Static Import Statements in Java

4. Static Block

Static blocks are portion of class initialization code, which are wrapped with static keyword.

public class Main {
     
    //static initializer
    static {
        System.out.println("Inside static initializer");
    }   
}

Static blocks are executed when the class is loaded in the memory. A class can have multiple static blocks and these will be executed in the same sequence in which they appear in class definition.

import static java.lang.System.out;

class DataObject 
{
	public Integer nonStaticVar;
	public static Integer staticVar;	//static variable
	
	//It will be executed first
	static {
		staticVar = 40;
		//nonStaticVar = 20;	//Not possible to access non-static members
	}
	
	//It will be executed second
	static {
		out.println(staticVar);
	}
}

Output:

40

5. 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 class.

public class JavaStaticExample 
{
	public static void main(String[] args) 
	{
		//Static inner class example
		System.out.println( DataObject.StaticInnerClas.innerStaticVar );
	}
}
class DataObject 
{
	public Integer nonStaticVar;
	public static Integer staticVar;	//static variable
	
	static class StaticInnerClas {
		Integer innerNonStaticVar = 60;	
		static Integer innerStaticVar = 70;		//static variable inside inner class
	}
}

Please note that an static inner class cannot access the non-static members of outer class. It can access only static members from outer class.

public class JavaStaticExample 
{
	public static void main(String[] args) 
	{
		//Static inner class example
		DataObject.StaticInnerClas.accessOuterClass();
	}
}
class DataObject 
{
	public Integer nonStaticVar;
	public static Integer staticVar;	//static variable
		
	static {
		staticVar = 40;
		//nonStaticVar = 20;	//Not possible to access non-static members
	}

	public static Integer getStaticVar(){
		return staticVar;
	}
	
	static class StaticInnerClas 
	{	
		public static void accessOuterClass()
		{
			System.out.println(DataObject.staticVar);		//static variable of outer class
			System.out.println(DataObject.getStaticVar());	//static method of outer class
		}
	}
}

Output:

40

6. Summary

Let’s summarize everything about static keyword usage in Java.

  1. Static members belong to class. No need to create class instance to access static members.
  2. Static members (variables and methods) can be accessed inside static methods and static blocks only.
  3. Non-static members cannot be accessed inside static methods, blocks and inner classes.
  4. A class can have multiple static blocks and they will be executed in order they appear in class definition.
  5. A class can be static only if its declared as inner class inside outer class.
  6. Static imports can be used to import all static members from a class. These members can be referred without any class reference.

Happy Learning !!

References:

Class members
Nested classes

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Java Static – Variable, Method, Block, Class and Import Statement”

  1. Excellent post,

    Your article provides a good and very detailed explanation about static keyword with best suitable programs. It will be very helpful for Java beginners. It is very important to learn the use of the static keyword in Java. The static keyword is used for memory management. We can use the static keyword with variable, methods, block and inner class. I also have a blog(java2blog), which provides complete core java tutorial and important interview questions and answers.

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.