Static Modifier
In this article, we will discuss the static non-access modifier. Non-access modifiers change the default behavior of Java class and its members.
The non access modifier static can be used with variables, methods, class, and interfaces and is explained below.
Static Variables
As we know, variables are associated with the class. Similarly, static variables belong to the class, and they are common to all the instances (reference variable of a class) and is shared by all the objects of the class. If we don't want a variable to be object-specific, we can make it as static.
Let us see this with an example,
package StaticModifier;
class Employee {
int empid;
String empName;
String ceo;
void display() {
System.out.println(empid + " : " + empName + " : " + ceo);
}
}
public class StaticDemo {
public static void main(String[] args) {
Employee emp1 = new Employee();
emp1.empid = 1;
emp1.empName = "ABC";
emp1.ceo = "XYZ";
Employee emp2 = new Employee();
emp2.empid = 2;
emp2.empName = "DEF";
emp2.ceo = "XYZ";
emp1.display();
emp2.display();
}
}
When this program executes, it will result in
1 : ABC : XYZ 2 : DEF : XYZ As in one organization CEO is the same for all the employees, so we will change the visibility of the variable CEO to static as its values are the same for all objects irrespective of the object.
The above scenario can be implemented as,
package StaticModifier;
class Employee {
int empid;
String empName;
static String ceo;
void display() {
System.out.println(empid + " : " + empName + " : " + ceo);
}
}
public class StaticDemo {
public static void main(String[] args) {
Employee emp1 = new Employee();
emp1.empid = 1;
emp1.empName = "ABC";
emp1.ceo = "XYZ";
Employee emp2 = new Employee();
emp2.empid = 2;
emp2.empName = "DEF";
emp2.ceo = "XYZ";
//this value will be replaced as ceo is a static variable
emp2.ceo = "PQR";
emp1.display();
emp2.display();
}
}
When this program executes, it will result in,
1 : ABC : PQR
2 : DEF : PQR
Since CEO is a static variable, it means the value will be the same for all objects irrespective of the object. Hence we get PQR as CEO name attribute.
Points to remember
Since static variables belong to a class, we can use both object name and class name - both will work. But the preferred way to access them is by using the class name.
If we make a variable static, it will not be any more for a particular object; it will be the same for all the objects.
Normal variables are loaded with a heap memory, but static variables are loaded with class loader memory.
If a variable is static, it means the value will be the same for all objects irrespective of the object.
Static Block
A static block is a special block in java that can be used to initialize a static variable, and it can be easily explained when referenced with static or non-static variables since the above code describes the need for the static keyword in Java.
Let us demonstrate it with an example,
package StaticModifier;
class Employee {
int empid;
String empName;
static String ceo;
//static block
static {
ceo = "XYZ";
}
public Employee() {
empid = 1;
empName = "ABC";
}
void display() {
System.out.println(empid + " : " + empName + " : " + ceo);
}
}
public class StaticBlockDemo {
public static void main(String[] args) {
Employee emp1 = new Employee();
Employee emp2 = new Employee();
emp1.display();
emp2.display();
}
}
When this program executes, it will result in
1 : ABC : XYZ
1 : ABC : XYZ
Note
Static Block will be executed only once irrespective of how many objects it will create.
Static Block is executed when you load a class; i.e., only once.
We cannot use a non-static variable in a static block.
Static Methods
As explained above, to access a static variable, we don’t need any object. Thus a method can be marked as static; thus, we don’t need any object to call a static method. These methods are not associated with objects.
static double average(int num1, int num2, int num3) {
return(num1+num2+num3)/3;
}
As everyone who wrote code in Java had interacted with this static method,
public static void main(String[] args) {
//some code
}
This main method is static, as referenced by the main class on its own.
A simple example can demonstrate this,
package StaticModifier;
class Employee {
int empid;
String empName;
static String ceo = "PQR";
//static block
Static void replace() {
ceo = "XYZ";
}
void display() {
System.out.println(empid + " : " + empName + " : " + ceo);
}
}
public class StaticBlockDemo {
public static void main(String[] args) {
Employee.replace(); //replace method is called
Employee emp1 = new Employee();
emp1.empid = 1;
emp1.empName = "ABC";
Employee emp2 = new Employee();
emp2.empid = 2;
emp2.empName = "DEF";
emp1.display();
emp2.display();
}
}
When this program executes, it will result in
1 : ABC : XYZ 2 : DEF : XYZ Points to remember
The static method can’t use a non-static variable or non-static method directly.
We can’t use this and super with static context.
We can use static methods and variables using a null reference.
We can’t override the static members in a derived class.
Static Classes and Interfaces
Static classes and interfaces are a type of nested class and interface in Java. Java uses the concept of nested class; i.e., a class defined under another class. The class in which the nested class is defined is the outer class.
As the inner class is capable of accessing all the members of the outer class, without using a reference to the outside class thus it makes it more concise and simple.
Let us demonstrate it with an example,
package StaticModifier;
public class StaticClass {
static class nestedClass {
public void show() {
System.out.println("This is a nested class.");
}
}
public static void main(String args[]) {
StaticClass.nestedClass nested = new StaticClass.nestedClass;
nested.show();
}
}
When this program executes, it will result in:
This is a nested class.
Points to remember
We can’t add static to the prefix of top-level class and interface.
For example,
static class Employee{}
static interface EmpInterface{}
This code will fail to compile as adding static with a prefix of class or interface is not allowed.
An inner class can access the member of outer class without its reference.
Summary
So at the end of this article, static keyword is important when the same value is used in the program again and again. It will not have any impact on objects, and if the variable is static, then its value will be the same for all objects irrespective of the object. The static method cannot use a non-static method or variable directly.
What did we learn?
Static Keyword in Java.
Use with variables and blocks.
Use with methods.
Use with Class and Interfaces.
Source: Microsoft
Comments