In this article we will look at static keyword which can be used to share data among multiple objects.
Introduction to static Keyword
In Java programs, static keyword can be used to create the following:
- Class variables
- Class methods
- Static blocks
Class Variables
The static keyword is used to create one of the three types of variables called as class variables. A class variable is a variable declared inside a class and outside all the methods and is marked as static. Syntax for declaring a class variable or a static variable is shown below:
static data-type variable-name;
Example for declaring a class variable is shown below:
static int id;
What is special about a class variable? As you already know, an instance variable is created separately for every object of the class. But a class variable is created only once inside the memory and the same is shared among all the objects of a class.
Consider the following code segment to demonstrate the difference between instance variables and class variables.
class CSEStudent
{
static int id = 5;
int stud_id;
String stud_name;
}
class StudentDemo
{
public static void main(String[] args)
{
CSEStudent s1 = new CSEStudent();
s1.stud_id = 5001;
s1.stud_name = "John";
CSEStudent s2 = new CSEStudent();
s1.stud_id = 5002;
s1.stud_name = "Kevin";
System.out.println("Branch id is: "+s1.id);
System.out.println("Branch id is: "+s2.id);
}
}
Below figure illustrates the difference between an instance variable and a class variable in the above code segment:
Class Methods
All the non-static methods inside a class are known as instance methods and all the static methods inside a class are known as class methods. A class method is generally used to process class variables. A class method has the following limitations:
- A class method (static method) can access only other class methods.
- A class method can access only class variables (static variables).
- this and super cannot be used in class methods.
A class method can be created using the following syntax:
static return-type method-name(parameters-list)
{
//statements
}
An example for creating a class method is shown below:
static void changeID(int newid)
{
id = newid;
}
Following code segment illustrates the use of class methods in Java:
class CSEStudent
{
static int id = 5;
int stud_id;
String stud_name;
static void changeID(int newid)
{
id = newid;
}
}
class StudentDemo
{
public static void main(String[] args)
{
CSEStudent s1 = new CSEStudent();
s1.stud_id = 5001;
s1.stud_name = "John";
CSEStudent s2 = new CSEStudent();
s1.stud_id = 5002;
s1.stud_name = "Kevin";
System.out.println("Branch id is: "+s1.id);
System.out.println("Branch id is: "+s2.id);
s1.changeID(2);
System.out.println("Branch id is: "+s1.id); //Guess the output
System.out.println("Branch id is: "+s2.id); //Guess the output
}
}
Static Blocks
A static block is a block of statements prefixed with static keyword. The syntax for creating a static block is shown below:
static
{
//Statements
}
An important property of a static block is, the statements in a static block are executed as soon as the class is loaded into memory even before the main method starts its execution. A typical use of static blocks is initializing the class variables.
Following program demonstrates the use of static blocks:
class CSEStudent
{
static int id;
int stud_id;
String stud_name;
static
{
id = 5;
//other statements
}
}
class StudentDemo
{
public static void main(String[] args)
{
CSEStudent s1 = new CSEStudent();
s1.stud_id = 5001;
s1.stud_name = "John";
CSEStudent s2 = new CSEStudent();
s1.stud_id = 5002;
s1.stud_name = "Kevin";
System.out.println("Branch id is: "+s1.id);
System.out.println("Branch id is: "+s2.id);
}
}
Note: Another important property of static keyword is, any class variable (static variable) or a class method (static method) can be accessed directly without creating an object for the class using the following syntax:
ClassName.variablename;
or
ClassName.methodname( );
This is the reason why the main method is declared as static in every program so that the JVM can access it directly without creating an object for the class. Following program demonstrates the alternative syntax for accessing class variables and class methods:
class CSEStudent
{
static int id = 5;
int stud_id;
String stud_name;
static void changeID(int newid)
{
id = newid;
}
}
class StudentDemo
{
public static void main(String[] args)
{
CSEStudent s1 = new CSEStudent();
s1.stud_id = 5001;
s1.stud_name = "John";
CSEStudent s2 = new CSEStudent();
s1.stud_id = 5002;
s1.stud_name = "Kevin";
System.out.println("Branch id is: "+ CSEStudent.id);
s1.changeID(2);
System.out.println("Branch id is: "+ CSEStudent.id); //Guess the output
}
}
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply