Sometimes you want to define a class member so that its use is completely independent of any object of that class. Typically, a class member must be accessed through the object of its class, but you can create a member that can be used by itself without reference to a particular instance. You can create such a member by preceding the declaration of the member with the keyword static (static). If a member is declared static, it can be accessed before any object of its class is created without reference to any object. You can declare both the method and the variable as static. The most common example of a static member is main (). Because main () must be invoked when the program starts executing, it is declared static.
A variable declared as static is essentially a global variable. When an object is declared, it does not produce a copy of the static variable, but all instance variables of the class share the same static variable. The methods declared as static have the following limitations:
1. They can only invoke other static methods.
2. They can only access static data.
They cannot refer to this or super in any way (the keyword super is related to inheritance).
If you need to initialize your static variable by calculation, you can declare a static block that executes only once when the class is loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:
// Demonstrate static variables,methods,and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
Once the Usestatic class is loaded, all static statements are run. First, A is set to 3, then the static block executes (prints a message), and finally, B is initialized to a*4 or 12. Then call Main (), and main () calls meth (), passing the value 42 to X. 3 println () statements refer to two static variables A and B, and local variable x.
Note: It is illegal to refer to any instance variable in a static method.
The following is the output of the program:
Static block initialized.
x = 42
A = 3
b = 12
Outside of the class that defines them, static methods and variables can be used independently of any object. So you just add the number operator to the name of the class. For example, if you want to invoke a static method from outside the class, you can use the following generic format:
Classname.method ()
Here, ClassName is the name of the class that defines the static method in the class. As you can see, this format is similar to the format of calling a non-static method with an object reference variable. A static variable can be accessed in the same format-the class name is dotted with the number operator. This is a controlled version of how Java implements global functionality and global variables.
Here is an example. In main (), the static method CallMe () and static variable B are accessed outside their classes.
class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
The following is the output of the program:
A = 42
b = 99