When to use “static” keyword in Java

The static keyword can be used in 3 scenarios

  1. static variables
  2. static methods
  3. static blocks of code.

Lets look at static variables and static methods first.

static variable

  • It is a variable which belongs to the class and not toobject(instance)
  • Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables
  • A single copy to be shared by all instances of the class
  • A static variable can be accessed directly by the class name and doesn’t need any object
  • Syntax : <class-name>.<variable-name>

static method

  • It is a method which belongs to the class and not to the object(instance)
  • A static method can access only static data. It can not access non-static data (instance variables)
  • A static method can call only other static methods and can not call a non-static method from it.
  • A static method can be accessed directly by the class name and doesn’t need any object
  • Syntax : <class-name>.<method-name>
  • A static method cannot refer to “this” or “super” keywords in anyway

Side Note:

  • main method is static , since it must be be accessible for an application to run , before any instantiation takes place.

Lets learn the nuances of the static keywords by doing some excercises!

Assignment: To Learn working of static variables & methods

Step 1) Copy the following code into a editor

public class Demo{
 public static void main(String args[]){
 Student s1 = new Student();
 s1.showData();
 Student s2 = new Student();
 s2.showData();
 //Student.b++;
 //s1.showData();
 }
}

class Student {
int a; //initialized to zero
static int b; //initialized to zero only when class is loaded not for each object created.

 Student(){
 //Constructor incrementing static variable b
 b++;
 }

 public void showData(){
 System.out.println("Value of a = "+a);
 System.out.println("Value of b = "+b);
 }
//public static void increment(){
//a++;
//}

}

This code is editable. Click Run to Compile + Execute

Step 2) Save & Compile the code. Run the code as, java Demo.

Step 3) Expected output show below

java-static-variable

Following diagram shows , how reference variables & objects are created and static variables are accessed by the different instances.

java-static

Step 4) It is possible to access a static variable from outside the class using the syntaxClassName.Variable_Name. Uncomment line # 7 & 8 . Save , Compile & Run . Observe the output.

Step 5) Uncomment line 25,26 & 27 . Save , Compile & Run.

Step 6) Error = ? This is because it is not possible to access instance variable “a” from static method “increment“.

static block

The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the JVM

class Test{
  static{
  //code goes here
  }
}

A static block helps to initialize the static data members, just like constructors help to initialize instance members

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s