• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Static Keyword

January 18, 2014 //  by Krishna Srinivasan//  Leave a 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.

Category: JavaTag: Java Basics

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.

Previous Post: « OCPJP 6 Mock Exam – 9
Next Post: Java Keywords »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact