Sometimes you want to define a class member so that its use is completely independent of any object of the class. Generally, a class member must be accessed through its class object, but can create such a member, which can be used by itself without referencing a specific instance. Add the keyword static before the declaration of the member to create such a member. If a member is declared as static, it can be accessed before any object of its class is created without referencing any object. You can declare both methods and variables as static. The most common example of static members is main (). Because the main () must be called when the program starts execution, it is declared as static. Variables declared as static are actually global variables. When declaring an object, it does not produce a copy of the static variable, but all instance variables of this class share the same static variable. Methods declared as static have the following restrictions: · They can only call other static methods. · They can only access static data. · They cannot reference this or super in any way (the keyword super is related to inheritance, as described in the next chapter ). If you need to initialize your static variable through calculation, you can declare a static block, which is only executed once when the class is loaded. The following example shows that the class 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 usestat The IC class is loaded and all static statements are run. First, a is set to 3, then the static block is executed (print a message), and finally, B is initialized to a * 4 or 12. Then, call main () and main () to call meth () and pass the value 42 to X. The three println () Statements reference two static variables A and B, and the local variable X. Note: It is invalid to reference 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 the class that defines them, static methods and variables can be used independently of any object. In this way, you only need to add the number operator after the class name. For example, if you want to call a static method from outside the class, you can use the following generic format: classname. method () Here, classname is the class name and defines the static method in this class. As you can see, this format is similar to that used to call non-static methods through object reference variables. A static variable can access the -- class name plus vertex operator in the same format. This is a control version of how Java implements global functions and global variables. The following is an example. In Main (), the static methods callme () and static variables B are accessed outside of 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) ;}} below is the output of the program: A = 42 B = 99