In the review of the Java knowledge Point of the time found this blog, write a great reprint over. Http://www.cnblogs.com/dotgua/p/6354151.html
Static keyword 1. Modifying member Variables
In our usual use, the most common function of static is to modify the properties and methods of the class, let them become members of the class properties and methods, we usually use static decorated members called Class members or static members, this sentence is very strange, in fact, this is relative to the object's properties and methods. Take a look at the following example: (The program is not too bloated, temporarily regardless of access control)
public class Person {
String name;
int age;
Public String toString () {return
' name: ' + name + ', Age: ' + age;
}
public static void Main (string[] args) {person
p1 = new Person ();
P1.name = "Zhangsan";
P1.age = ten;
person P2 = new person ();
P2.name = "Lisi";
P2.age =;
System.out.println (p1);
System.out.println (p2);
}
/**output
* Name:zhangsan, age:10
* name:lisi, age:12
*///~
}
The code above is familiar to us, and each object constructed by person is independent, preserving its own independent member variables and not interacting with each other, and they are shown in memory as follows:
As you can see from the illustration above, the objects referenced by the P1 and P2 two variables are stored in different addresses in the memory heap area, so they don't interfere with each other. But in fact, in this, we omitted some important information, I believe you will also think that the object's member attributes are here, by each object to save their own, then their method. In fact, no matter how many objects a class creates, their methods are the same:
From the above diagram, we can see that the method of two person objects actually only points to the same method definition. This method is defined as a invariant region in memory (divided by the JVM), which we call the static storage area. This storage area not only holds the definition of the method, but it actually holds the definition of various classes from a larger perspective, and when we build the object from new, we create the object based on the definition of the class defined here. Multiple objects will only correspond to the same method, here's a good reason to be convinced that no matter how many objects their methods are always the same, although the final output will be different, but the method will always follow our desired results to operate, that is, different objects to invoke the same method, the result will be different.
We know that the Static keyword can modify member variables and methods to make them belong to the class, not to the object, such as we decorate the person's age attribute with static, and what the result would be. Take a look at the following example:
public class Person {
String name;
static int age;
/* The rest of the code is unchanged ... * *
/**output
* Name:zhangsan, age:12
* name:lisi, age:12
*///~
}
We find that the result has changed a little bit, and why it interferes with the age attribute of the P1 when assigning values to the P2. We're still looking at their signals in memory:
We find that after adding the static keyword to the age attribute, the person object no longer has the age attribute, and the age attribute is unified to the person class to manage, that is, multiple person objects will only correspond to an age attribute, and an object that changes the age attribute Other objects will be affected. We see that at this time the Age and ToString () methods are all managed by the class.
Although we see static allows objects to share properties, it is rarely used in practice and is not recommended. Because it makes this property difficult to control because it can be changed anywhere. If we want to share attributes, we will generally adopt other methods:
public class Person {
private static int count = 0;
int id;
String name;
int age;
Public person () {
id = ++count;
}
Public String toString () {return
"ID: + ID +", Name: "+ name +", Age: "+ age;
}
public static void Main (string[] args) {person
p1 = new Person ();
P1.name = "Zhangsan";
P1.age = ten;
person P2 = new person ();
P2.name = "Lisi";
P2.age =;
System.out.println (p1);
System.out.println (p2);
}
/**output
* id:1, Name:zhangsan, age:10
* id:2, Name:lisi, Age:12
*///~
}
The above code has the effect of creating a unique ID for the object of person and the total number of records, where count is decorated by static and is the member property of the person class, and each time a person object is created, the property is added 1 and then assigned to the object's ID attribute. The Count property records the total number of person objects created, and because Count uses private adornments, it cannot be changed from outside the class. 2. Cosmetic Member Method
Another function of static is to modify the member method. The cosmetic member method does not change much on the storage of the data, as we can see from the above that the method is stored in the definition of the class. The most important function of the static modifier member method is that you can use the method of "class name. Method name" to avoid the tedious and resource consumption of new objects first, and we may often see it in the Help class:
public class Printhelper {public
static void print (Object o) {
System.out.println (o);
}
public static void Main (string[] args) {
printhelper.print ("Hello World");
}
Here's an example (not quite practical yet), but we can see how it works, so that the static-decorated method becomes a method of class, making it easy to use using the "class name. Method Name", which is equivalent to defining a global function (as long as you import the package where the class resides). But it also has the limitations of its use, in a static-decorated class, you cannot use a member variable and a method that are not static-decorated, which is well understood, because the static-decorated method is part of the class, and if you go directly to the object's member variable, it is overwhelmed (not knowing which object's properties to use). 3. Static Block
When describing the third use of the static keyword, it is necessary to comb through the initialization of an object. Take the following code as an example:
Package com.dotgua.study;
Class book{Public book
(String msg) {
System.out.println (msg);
}
}
public class People {book
Book1 = new book ("BOOK1 member variable Initialization");
Static book Book2 = new book ("Static member BOOK2 member variable Initialization");
Public person (String msg) {
System.out.println (msg);
}
Book BOOK3 = new book ("BOOK3 member variable Initialization");
Static book Book4 = new book ("Static member BOOK4 member variable Initialization");
public static void Main (string[] args) {person
p1 = new Person ("P1 initialization");
/**output
* Static member BOOK2 member variable initialization
* Static member BOOK4 member variable initialization
* BOOK1 member variable initialization *
BOOK3 member variable initialization
* P1 initialization
*///~
}
In the example above, the person class combines four book member variables, two are regular members, and two are static-decorated class members. We can see that when we new a person object, the static-decorated member variable is initialized first, then the normal member, and finally the constructor of the person class is initialized. That is, when an object is created, the members of the static modifier are initialized first, and we can see that if there are multiple static-decorated members, they are initialized according to their position.
In fact, the initialization of the members of the static modifier can be done earlier, see the following example:
class book{public Book (String msg) {System.out.println (msg);
Book1 = new book ("BOOK1 member variable Initialization");
Static book Book2 = new book ("Static member BOOK2 member variable Initialization");
Public person (String msg) {System.out.println (msg);
Book BOOK3 = new book ("BOOK3 member variable Initialization");
Static book Book4 = new book ("Static member BOOK4 member variable Initialization");
public static void Funstatic () {System.out.println ("Funstatic Method of Static modification");
public static void Main (string[] args) {person.funstatic ();
System.out.println ("****************");
person P1 = new person ("P1 initialization");
}/**output * Static member BOOK2 member variable initialization * Static member BOOK4 member variable initialization * Static modified Funstatic method * *************** * BOOK1 member Variable initialization * BOOK3 member variable initialization * p1 initialization *///~}
In the example above we can find two interesting places, the first is that when we do not create an object, but call a class method by a class, although the method does not use any of the class members, the class member is initialized before the method call, which means that when we first use a class, Triggers the member initialization of the class. The second is when we use the class method, after the initialization of the members of the class, and then new objects of the class, the static-decorated class members are not initialized again, which means that the static-decorated class members, in the process of running the program, only need to initialize once, not multiple initialization.
After reviewing the initialization of the object, it is very easy to see the third effect of the static, that is, when we initialize the members of the static modifier, we can unify them in a block statement wrapped in curly braces with a static start: