Static keywords in java and javastatic keywords
Study Notes, materials from:
Http://www.cnblogs.com/dolphin0520/p/3799052.html
Static keywords can be added to methods, member variables, and code blocks. Class.
1. static Method
The static method can be accessed directly without class instantiation. In static methods, non-static member variables and non-static member methods cannot be called because non-static member methods/variables must depend on specific objects.
2. static variables
Static variables are also called static variables. The difference between static variables and non-static variables is that static variables are shared by all objects and have only one copy in the memory, it is initialized only when the class is first loaded. Non-static variables are owned by objects. They are initialized when an object is created and have multiple copies. The copies of each object do not affect each other.
Static member variables are initialized in the defined order.
3. static code block
It can be used to initialize some member information.
Public class testClassA {public static int val = 1; static {System. err. println ("11:" + val); val = 2; System. err. println ("22:" + val);} public static int getVal () {return val ;}} System. err. println (testClassA. getVal ());
Result:
Notes:
1. static member variables can be accessed by class objects after instantiation, and static methods can also be accessed by objects, but there is a warning.
2. Local variables cannot be static.
3. The static code block takes precedence over the constructor.