Static method
Typically, a method is defined in a class for static, that is, this method can be called without the object of this class. As follows:
Class Simple {static void Go () {system.out.println ("Go ...");}}}} public class cal {public static void main (String [] args) {Simple.go ();}}
Calling a static method is "Classification. Method Name", the use of static methods is simple as shown above. In general, static methods often provide some utility to other classes in the application, and a large number of static methods in Java libraries are defined for this purpose.
Static variable
Static variables are similar to static methods. All such instances share this static variable, that is, when the class is loaded, only one storage space is assigned, all such objects can be manipulated this block storage space, of course, for Final. Look at the following code:
Class value {static int C = 0; static void inc () {C ;}} class count {public static void prt (string s) {system.out.println (s);} public static void main (String [] args ) {Value V1, V2; V1 = new value (); v2 = new value (); PRT ("v1.c =" v1.c "v2.c =" v2.c); v1.inc () ; PRT ("v1.c =" v1.c "v2.c =" v2.c);}}
The results are as follows:
v1.c = 0 v2.c = 0V1.c = 1 v2.c = 1
This can be proved to share a memory area. The Static variable is a bit similar to the concept of global variables in C. It is worth exploring the initialization problem of static variables. We modify the above procedures:
Class value {static int C = 0; value () {c = 15;} value (int i) {c = i;} static void inc () {C ;}} class count {public static void PRT (String s) {System.out.println (s); value v = new value (10); static value V1, v2; static {PRT ("v1.c =" v1.c "v2.c =" v2.c ); V1 = new value (27); PRT ("v1.c =" v1.c "v2.c =" v2.c); v2 = new value (15); PRT ("v1.c =" v1.c "v2.c =" v2.c);
Public static void main (string [] args) {count ct = new count (); PRT ("ct.c =" ct.vc); PRT ("v1.c =" v1.c "v2.c = " v2.c); v1.inc (); PRT (" v1.c = " v1.c " v2.c = " v2.c); PRT (" ct.c = " ct.vc) }} The results of the operation are as follows:
v1.c = 0 v2.c = 0v1.c = 27 v2.c = 27v1.c = 15 v2.c = 15ct.c = 10v1.c = 10 v2.c = 10V1.c = 11 v2.c = 11CT .c = 11
This program shows the various features of static initialization. The first thing to tell you is that the variable defined by static is preferred over any other non-Static variable, regardless of its order. As is shown in the program, although V appears in front of V1 and V2, the result is the initialization of V1 and V2 in front of V. At the static {followed by a code, this is used to initialize explicit static variables, which only initializes once, and when the class is loaded. If you can read and understand this code, you will help you understand the Static keyword. When it is involved in inheritance, it will initially initialize the static variable of the parent class, then the subclass is pushed. You can refer to THINK IN JAVA explanation.
Static class
Usually a normal class is not allowed to be static, only one internal class can be. At this time, this statement is that the static internal class can be used directly as a normal class without an example of an external class. The following code shows:
public class StaticCls {public static void main (String [] args) {OuterCls.InnerCls oi = new OuterCls.InnerCls ();}} class OuterCls {public static class InnerCls {InnerCls () {System.out.println ( "InnerCls" }}}
Output results:
InnerCls