Java Static keyword example
The static
keyword in Java is a modifier used to create memory efficient code. It helps in managing the memory occupied by objects, variables and method definitions. The static keyword makes sure that there is only one instance of the concerned method, object or variable created in memory.
It is used when one needs to have a class level variable to manage certain attribute value and also when one needs a class level method to perform certain operations. The use of the static keyword is discussed in each content in detail below.
1. STATIC keyword as a variable modifier
Static keyword, when used for variables, indicates that a variable should be instantiated only once per class. It occupies one memory unit as per its datatype irrespective of number of objects being created. Thus, the value of static keyword is common across every object of the particular class.
A static variable is a type of variable which does not require an object instance in order to access it. It can directly be accessed using syntax <classname>.<variablename>
Let us understand it with an example:
The below code is a class holding certain details of an Employee. It contains a static variable “count” which is being used to count the number of employees being created.
package com.mkyong;
public class Employee {
static int count = 0;
private String name;
private int leaves;
Employee() {
count++;
}
Employee(String name, int leaves) {
this.name = name;
this.leaves = leaves;
count++;
}
public static int getCount() {
return count;
}
public static void setCount(int count) {
Employee.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLeaves() {
return leaves;
}
public void setLeaves(int leaves) {
this.leaves = leaves;
}
}
The below class is a test class created to test the functionality and working of a static variable. On executing the code, you can see that the value of static variable “count” is shared across all the employee objects.
package com.mkyong;
public class StaticVariableExample {
public static void main(String[] args) {
Employee e1 = new Employee("Employee1", 5);
Employee e2 = new Employee("Employee2", 10);
System.out.println("Employee Count using e1: " + e1.getCount());
System.out.println("Employee Count using e2: " + e2.getCount());
System.out.println("Employee Count using static reference: " + Employee.getCount());
}
}
In the Employee class, “count” is a static variable which has been instantiated just once per class. Thus, irrespective of the count of objects that we create, the value of “count” is shared across all the objects which can clearly be seen in the output.
On running the StaticExample class, output is obtained as shown below:
Employee Count using e1: 2
Employee Count using e2: 2
Employee Count using static reference: 2
2. STATIC keyword as a method modifier
A static method in Java is used when the developer wishes to expose the method directly to the user i.e without the need to create an actual object for invoking the method. Static methods can be directly invoked using the syntax <classname>.<methodname>
.
A static method can act only on static variables and objects. In addition to that, any method call done by a static method should be referencing a static method in turn.
Let us understand this with an example:
package com.mkyong;
public class StaticMethodExample {
private int x = 0;
private static int y = 0;
public static void main(String[] args) {
// If uncommented, x++ gives compilation error
// x++;
// y++ works fine without compilation error
y++;
// If uncommented, sayHello() gives compilation error
// sayHello();
// sayHi() works fine without compilation error
sayHi();
System.out.println("Value of y is: " + y);
}
public void sayHello() {
System.out.println("Hello");
}
public static void sayHi() {
System.out.println("Hi");
}
}
Please note the comments also in the above code. They specify the contexts where you may get compilation errors on referencing non-static variable or method from a static function. On executing the above code, we get the following output.
Hi
Value of y is: 1