1.static can be used to decorate member variables, methods, and blocks without altering the permissions adornments of members in a class, such as private decorated member variables, which can only be called by class names or non-private method calls, and not by object names.
2.static methods and member variables, which can be called directly using the class name, without instantiation: Class name. Method name (); class name. variable name ();
It can also be invoked using the object name after it is instantiated.
3.static method (static method):
3.1 Static methods, no this, because no object was created;
3.2 Static methods cannot invoke non-static member variables, methods, but non-static methods can call static member variables, methods;
4.static variable (static variable):
4.1 Static variables are initialized and assigned when the class is loaded for the first time;
4.2 Each static variable in memory is only one copy, all objects share this copy, no matter which object or the class name call modification, will make it change;
4.3 Static cannot be used to modify local variables, as defined by the Java syntax.
5.static BLOCK:
5.1 Static blocks can be defined anywhere in the class, called once when the class first loads, and called only once, that is, the object has not yet been created:
Public classTestextendsbase{Static{System.out.println ("Test Static"); } PublicTest () {System.out.println ("Test constructor"); } Public Static voidMain (string[] args) {NewTest (); }} classbase{Static{System.out.println ("Base Static"); } PublicBase () {System.out.println ("Base constructor"); }}
Staticstaticbase Constructortest constructor
View Code
Execution process: First to find the main method, because the main method is the entry of the program, but before executing the main method, the test class must be loaded, and when the test class is loaded to find that the test class inherits from the base class, so will go to load the base class, when loading the base class , and a static block is found, the static block is executed. After the base class is loaded, the test class continues to load, and then the static block is executed when the test class is found with a static block. After the required classes have been loaded, the main method is executed. When you execute new Test () in the main method, the constructor of the parent class is called first, and then the constructor for itself is called. As a result, the above output appears.
The static of the Java learning Note