^

Static Members

Variables and methods declared using keyword static are called static members of a class. You know that non-static variables and methods belong to instance. But static members (variables, methods) belong to class. Static members are not part of any instance of the class. Static members can be accessed using class name directly, in other words, there is no need to create instance of the class specifically to use them.
Static members can be of two types:
#Static Variables
#Static Methods

Static Variables

Static variables are also called class variables because they can be accessed using class name, whereas, non static variables are called instance variables and can be accessed using instance reference only. Static variables occupy single location in the memory. These can also be accessed using instance reference.
These are accessible in both static and non-static methods, even non-static methods can change their values.

While declaration, there is no need to initialize the static variables explicitly because they take default values like other non-static variables (instance variables).
Now, the question is, why to use static variables?

Let us look at a reason for using one. Suppose you want to keep record about the number of running instances of a class. In this situation, it would be better to declare a static variable.

Static Methods/Class Methods

Static methods are also called class methods. A static method belongs to class, it can be used with class name directly. It is also accessible using instance references.

Static methods can use static variables only, whereas non-static methods can use both instance variables and static variables.

Generally, static methods are used to perform some common tasks for all objects of a class. For example, a method returns a random number. It does not matters which instance use this method, it will always behave exactly in this way. In other words, static methods are used when the behavior of a method has no dependency on the values of instance variables.
Implementation:
The following program uses a static counter variable and static method showCount() method to track the number of running instances of the class student in the memory.
class Student
	{     private static int count=0; //static variable
	       Student()		
		{ 
		       count++;	//increment static variable
		}
	       static void showCount() //static method
		{
		       System.out.println(“Number Of students : “+count);
		}
	}
public class StaticDemo
         {    
	    public static void main(String[] args)
	        {
		Student s1=new Student();
		Student s2=new Student();
		Student.showCount(); //calling static method
		Student s3=new Student();
		Student s4=new Student();
		s4.showCount();	//calling static method
	       }
	}
 Output:
            Number of students : 2
            Number of students : 4
Explanation:
In the above program, constructor function increments the static variable (count) on creation of new object of Student class. The program also consists a static method i.e. showCount(), which is invoked to get number of objects of Student class in memory.

About the Author
Rajesh K. Bansal (SCJP-Sun Certified Java Programmer)
20 Years experience in Training & Development. Founder of realJavaOnline.com, loves coding in Java(J2SE, J2EE), C++,PHP, Python, AngularJS, Android,MERN Stack(MongoDB,Express,ReactJS,NodeJS). If you like tutorials and want to know more in depth about Java , buy his book "Real Java" available on amazon.in.
#Email : bcebti@gmail.com #Contact : 98722-46056
Available on Amazon
Card image cap
Under the guidance of Founder & Author of "realJavaOnline.com". M:9872246056
Card image cap