1, static means "global" or "static" meaning, used to modify the member variables and member methods, you can also form static code block, the static modified member variables and member methods independent of any object of the class. That is, it does not depend on class-specific instances, is shared by all instances of the class (so it can be used to count how many instanced objects a class has), so some properties want to be shared by all objects, and must be declared as a static property, and the property declared by static is a global property.
2. As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the runtime data area. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.
3. Static code blocks defined in a class take precedence over the construction block, and no matter how many objects, static code blocks execute only once
Example 1:
Class persone{
private String name;
private int age;
private static String country = "a city";
Public Persone (String Name,int age) {
THIS.name = name;
This.age = age;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
public static String Getcountry () {
return country;
}
public static void Setcountry (String country) {
Persone.country =country;
}
public void info () {
System.out.println ("Name:" +this.getname () + "," + "Age:" +this.getage () + "," + "City:" +country ");
}
}
public class Staticuse {
public static void Main (string[] args) {
Persone per1 = Newpersone ("Zhang San", 20);
Persone per2 = Newpersone ("John Doe", 22);
System.out.println ("--------before---------modification");
Per1.info ();
Per2.info ();
System.out.println ("---------after---------modification");
Persone.setcountry ("b City");
Per1.info ();
Per2.info ();
}
}
Static Continuation--