Sometimes you want to define a class member so that its use is completely independent of any object of that class . Typically, the class
a member must be accessed through the object of its class, but can create such a member that it can be used by itself,
instead of referencing a specific instance. Add the keyword static (static) before the member's declaration to create such a
members. If a member is declared static, it is able to be accessed before any object of its class is created .
You do not have to reference any objects. You can declare methods and variables as static. The most common of the static members
The example is main (). Because main () must be called when the program starts executing, it is declared as static.
a variable declared as static is essentially a global variable. When an object is declared, it does not produce a static variable.
copy, but all instance variables of the class share the same static variable. The method declared as static has the following
Restrictions:
• They can only invoke other static methods.
• 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, a static block
executes only once when the class is loaded. The following example shows a class that has a static method, some static changes
volume, 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 (a);
}
}
once the Usestatic class is loaded, all static statements are run. First, A is set to 3 and then
the static block executes (prints a message), and finally, B is initialized to a*4 or 12. Then call Main (), main
() call meth () and pass the value 42 to X. 3 println () statements refer to two static variables A and B to
and the local variable x.
Note: Referencing any instance variable in a static method "instance variable, field, member variable, property is equivalent" is illegal.
The following is the output of the program:
Static block initialized.
x =
A = 3
B =
outside of the classes that define them, static methods and variables can be used independently of any object. This way, you only
to be in the class's
After the name, add the number operator. For example, if you want to call a static party from outside the class
method, you can use the following common format:
Classname.method ()
here, ClassName is the name of the class, and the static method is defined in the class. As you can see, this format
an object reference variable calls a non-static method in a similar format. A static variable can be visited in the same format
Q-The class name dot operator. This is a control version of how Java implements global functionality and global variables
this.
here is an example. In main (), the static method CallMe () and the static variable B are outside their class
be accessed.
class Staticdemo {
static int a =;
static int b =;
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 =
B =
Static "Method" "Variables" "Blocks" in Java