Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Core Java Basics. No Comments on static Keyword
2.3
(3)

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:

  1. Class variables
  2. Class methods
  3. 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:


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

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:

 

Instance variables vs static variables

 

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:

  1. A class method (static method) can access only other class methods.
  2. A class method can access only class variables (static variables).
  3. 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
	}
}

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

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.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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