What is Java static keyword, static variable, static method?

Java static is keyword used as an access modifier. It modifies the accessing technique of variables and methods in Java coding. "static" keyword can be applied to variables, methods, inner classes and blocks.

We know, to call an instance variable and method, Java requires an object (and thus Java maintains encapsulation). But declaring a variable and method as static, an object is not required to call. That is, a static variable and static method can be called without the help of an object. It is the facility static gives but we loose encapsulation. Of course, it is not a minus point as sometimes between objects encapsulation is not required. In this tutorial, we discuss each concept clearly.

A Java static variable or method can be called in three ways.

  1. without object
  2. with object
  3. with class name

1. Using Java static variable

public class Employee
{
  String name = "Rao";			        // non-static and requires an object to call
  static int salary = 7500;			// static and does not require an object 
					        // and can be called in three ways
  public static void main(String args[])
  {				// USING NON-STATIC VARIABLE
    System.out.println("Calling non-static variable name:");
    // System.out.println(name);		// error as object is required
    Employee emp1 = new Employee();		// now create an object
    System.out.println(emp1.name);		// its fine and works   
  				// USING STATIC VARIABLE IN THREE WAYS
    System.out.println("\nCalling static variable salary in three ways:");
    System.out.println(salary);    		//  calling without object
    System.out.println(emp1.salary);    	//  calling with object
    System.out.println(Employee.salary);    	//  calling with class name
  }
}

Java static
The code is well commented. The same case with methods also and let us see one example.

2. Using Java static methods

public class Employee
{
  public void display()			        // non-static method and requires an object to call
  {
    System.out.println("Hello 1");
  }  
  public static void show()		        // static method and requires NO object to call
  {				                // and can be called in three ways
    System.out.println("Hello 2");
  }  

  public static void main(String args[])
  {				// USING NON-STATIC VARIABLE
    System.out.println("\nCalling non-static method display() method:");
    // display();				// error as object is required
    Employee emp1 = new Employee();		// now create an object
    emp1.display();				// its fine and works   
  				// USING STATIC VARIABLE IN THREE WAYS
    System.out.println("\nCalling static method show() in three ways:");
    show();		    			//  calling without object
    emp1.show();		    		//  calling with object
    Employee.show();		    		//  calling with class name
  }
}

Java static
Why static variable is called a “class variable”?

3. Does static maintain Encapsulation?

First let us write an example and then discuss.

public class Employee
{
  static int salary = 7500;
		
  public static void main(String args[])
  {		
    System.out.println(salary);			// pritns 7500

    Employee emp1 = new Employee();		// create two objects
    Employee emp2 = new Employee();

    System.out.println(emp1.salary);		// prints 7500
    System.out.println(emp2.salary);		// prints 7500

    emp1.salary = 8500;			        // change emp1 salary
    System.out.println(emp2.salary);    	//  emp2 salary also gets effected; prints 8500

    emp2.salary = 9500;			        // change emp2 salary
    System.out.println(emp1.salary);    	//  emp1 salary also gets effected; prints 9500

    System.out.println(salary);    		//  prints 9500
  }
}

Java static

In the above code, observe the screenshot, if emp1 salary changes emp2 salary is also gets effected and similarly when emp2 salary is changed, emp1 salary gets effected. This is not encapsulation. With static variable encapsulation is not maintained. Why it is so? It is because the same salary variable (or address) is referred (or pointed) by all the objects of the class. That is, all objects of the whole class uses the same location. For this reason, static variable is known as class variable. Note that, with non-static variable, each object maintains its own location (or copy) separately.

4. When to use Java static variables?

  1. When encapsulation is not required. For example, three partners exist in a business with a joint account. If the balance in the account is 1 lakh, if one partner draws one lakh, other partners cannot draw one lakh again. That is, between all the partners there should be a common balance variable and it is achieved with static keyword as static balance.
  2. Many methods of many classes in Java API are declared static to allow the Programmer to call them without then need of creation of object. Ex: Character and Math etc.

Leave a Comment

Your email address will not be published.