Static members of a class
When we use a “static” modifier with the declaration of a member of a class is called a static member of that class. It is also called a class member.
We can use static with a variable, method, inner class, and blocks. Static members are associated with the class, not with the class object. Static members can be used without instantiating the class i.e. creating an object of a class. The reason behind this is that all static members of a class are loaded into memory when the class is loaded in memory. Non-static members of a class are loaded into memory for each instantiation of the class.
We can access any static member of a class using its class name as a reference. We can call static members with an instance of a class as well which is not recommended.
A static variable of a class is shared by each instance of that class. Any object of the class can modify the value of a static variable which will impact all other objects of a class as well as it is a shared variable. The static member variable can be modified without any object as well.
Let’s learn accessing static members of a class through a simple java program.
package JavaConcepts;
public class StaticMembers {
// Static variable
public static String name = "Selenium";
// Non static variable
public int version = 3;
// Static method
public static void printName()
{
System.out.println("Name is :"+name);
}
// Non static method
public void printVersion()
{
System.out.println("Version is : "+version);
}
public static void main(String[] args) {
// Can call static members with class name
System.out.println(StaticMembers.name);
StaticMembers.printName();
// Can not call non static members with class name
//System.out.println(StaticMembers.version);
//StaticMembers.printVersion();
// Creating an object of class. We can call members using object name
StaticMembers sm = new StaticMembers();
// Calling static member with object is not recommended
System.out.println(sm.name);
sm.printName();
// Object name should be used to call non static members of class
System.out.println(sm.version);
sm.printVersion();
}
}
Output
Selenium
Name is :Selenium
Selenium
Name is :Selenium
3
Version is : 3
n the above example program, we called static members of the class using its class name. When we have multiple calls then every time we need to use its class name as a reference. We may end up with multiple boilerplate codes and reduced code readability. We can solve these problems using the static import concept of Java.
Static Import in Java
Java introduced the static import concept in 1.5. Static import allows public and static members i.e. fields and methods of a class to be used in Java code without specifying the class name.
Syntax
If we want to import all public and static members of a class:-
import static pkg1.classname.*;
e.g. import static java.lang.Math.PI;
If we want to import a specific member of a class:-
import static pkg1.classname.someMethod;
e.g. import static java.lang.Math.*;
When to use static import?
Avoid it as much as possible. Static import is a good option when you have 1-2 classes to call static members from. If you have multiple classes from where you need to call methods then instead of increasing readability it will reduce it and it will be confusing for others and for ourselves as well.