Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The static members

Usually, the class members that we declare are used with object’s reference to making them execute. Such members are known as the instance variable/members. They execute separately for each instance/object because each object has a separate copy of class members.

Example for instance variable

class XYZ 
{
    int x;
    int y;
	
    public void printValues() 
    {
        System.out.println("x = "+x);
        System.out.println("y = "+y);
    }
	
    public static void main(String args[]) 
    {
        XYZ obj1 = new XYZ();
        XYZ obj2 = new XYZ();
		
    //  x = 5;  cannot accessed without object's reference
    //  y = 6;  because x & y are not static
		
        obj1.x = 5;  // variables accessed using object's reference
        obj1.y = 6;  
		
        obj2.x = 10; // variables accessed using object's reference
        obj2.y = 20;
		
        System.out.println("obj1 variables :: ");
        obj1.printValues();  // method accessed using object's reference
		
        System.out.println("obj2 variables :: ");
        obj2.printValues();  // method accessed using object's reference
		
    //  printValues();  cannot accessed without object's reference
    //                  because printValues() is not static
    }
}

Output

obj1 variables ::
x = 5
y = 6
obj2 variables ::
x = 10
y = 20

In the above program, you can notice that if we want to access class members x, y or printValues() method, we used an object’s reference to invoke them because they are instance members.

Java Programming Language Static Members Example

It is also possible to access class members without object’s reference. For that, we need to declare class members as a static by using static keyword.

After declaring a class member as a static, it can be accessed/invoked without using object’s reference.

It is possible to make variable & methods as static members.

The static member can be accessed before object’s creation.

The main() method is always defined as static because it needs to be invoked before any object creation.

The static members of a class are used in common among all the objects. Whereas new instance variables are created for each new object.

Instance variable

Java Programming Language Instance Variable Example

Static variable

Java Programming Language Static Variable Example

Example for static members

class StaticDemo 
{ 
    static int x;
    static int y;
	
    public static void printValues()
    {
        System.out.println("x = "+x);
        System.out.println("y = "+y);
    }
	
    public static void main(String args[]) 
    {
        x = 10;         // accessed without object’s reference
        y = 20;         // accessed without object’s reference
        printValues();  // accessed without object’s reference
    }
}

Output

x = 10
y = 20

The static member can be accessed directly from the other class by using class name in which it resides.

Example

className.staticMember;

It’s like using child name along with its parent’s name to recognize to whom child belongs.

Limitations of the static method

The static method can access only static methods and static variables directly.

The static method cannot use this or super keywords.

 

Leave a reply