Static Keyword

SHARE & COMMENT :

Static keyword in Java used for indicating the members are belong to the class and not to the instance. It means that, if you declare a static members, it is shared among all the instances of that class. Also note that the static members are initialized before the instance creation of that class.

Static keyword can be used with the following scenarios:

  • Methods
  • Variables
  • Initializer block
  • Inner classes

Static Methods

  • If you declare a method as static, it is bound to the class and it can be accessed without creating an instance.
  • it can not access the not static methods and variables directly since static context is created before instance is created.

Static Variables

  • A static variable is shared by all the instances of a class.
  • A class can have multiple instances by using the new keyword. Every instance have its own instance variables. But, the static variables are shared by all the instances.
  • You have to be wise in using the static variables, since this value is shared with entire application which may cause undesirable result when used in the concurrent user environment.
  • If the variable is not defined as static, by default it is instance variable.

Static Initializer Blocks

  • These are simple initializer blocks inside a class with the static keyword. These are invoked when the class is loaded at first time to initialize the variables inside this block.
  • Note that only static variables can be initialized inside this block. If you want to initialize the instance variables, you can use the non-static initializer block.
  • Static initializers are the perfect place to initialize class level variables which has to be used before instance is created.

Inner Classes

  • Inner classes are special class which are declared inside a class. These are known as the nested classes. These classes are declared as static.

Static Keyword Example

StaticExample.java

package javabeat.net.core;

public class StaticExample {
	//Static variable declaration
	static private int counter;

	//Instance variable
	private int instanceVar;

	//Static initializer to initialize variable
	static{
		counter = 0;

		//Compiler error - Instance variable not allowed as below
		//instanceVar = 0; -
	}

	//Static method which access statuc variables
	static public void printCounter(){
		System.out.println("Counter : " + counter);

		//Compiler error. Instance variable not allowed as below
		//System.out.println("Counter : " + instanceVar);
	}

}

StaticDemo.java

package javabeat.net.core;

public class StaticDemo {
	public static void main(String args[]){
		//Using class name we can access static members
		StaticExample.printCounter();
	}
}

If you look at the example, it is easy to understand the use of static keyword in the various situations. If you have any questions, please write it in the comments section.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better