Java is an object-oriented language. It must be organized as an object during development.Code, Methods and attributes should belong to an object, rather than independent.
There is also a class of methods and attributes in Java. They do not belong to a specific object, but are shared by multiple objects. They are called static methods or static attributes.
1. Static attributes
Static attributes are shared by all objects of a class and are defined by the static keyword. The following code defines a static property:
Public class person (
Public static int count;
)
To access static properties, you can use the object name or the class name. The following code is correct:
Method 1:
Person. Count = person. Count + 1;
Method 2:
Person = new person ();
Person. Count ++;
This is different from non-static members. Non-static members can only be called by object names. Each object has its own attributes. For example, Zhang San has its own height and Li Si has its own height. Because static members correspond to classes, rather than static members correspond to objects, each object will have one. Assume that the person class instantiates six objects. For static members, there is only one space in the memory to save this attribute, and 6 non-static members have one object. Therefore, static members are also called class members, and non-static members are called object members.
Example: Write the output result of the following code
Person p1 = new person ();
P1.count = 10;
Person P2 = new person ();
P2.count = 20;
System. Out. println (p1.count );
System. Out. println (p2.count );
Answer:20 20Click to view the answer.
2. Static Method
Static Methods belong to class methods and do not belong to a specific object. Therefore, static methods cannot be accessed.NonStatic member.
Static Method definition, using the keyword static, the following defines a static method to operate on count:
Public static void setcount (INT count ){
Person. Count = count;
}
Public static int getcount (){
Return count:
}
In fact, the static method has been used at the beginning of the course. Do you remember the definition of the main method?
The static method cannot access non-static attributes. The following code is incorrect:
Public int height;
Public static int getheight (){
Return height;
}
Q: When should I use static methods?
Answer:If the object attributes (non-static member variables) are not used during method execution, but static member variables are used or the information passed in through parameters, the processing process of the method is irrelevant to the object, therefore, you can set static methods and static methods.
Click to view the answer.
3. Static initializes
The constructor is used to initialize member variables. static member variables do not belong to a specific object. How can we initialize static member variables? Use the static initializer. The following is a piece of static initializing code:
Static {
Count = 0;
}
A pair of braces in the static guide. The braces contain the initialization code for the static members. This is the static initialization tool. It can be considered as a special method with no return value type and no parameters, there is no method name, because it is called when the class is loaded.
Note: Only static members can be operated on in the static initiator, but not non-static members.
Next we will introduce a simple object-oriented example.
Last time:
31st constant Member Next: 33rd parent class and inheritance Li xucheng csdn blog: Http://blog.csdn.net/javaeeteacher Invite you as a friend:Http://student.csdn.net/invite.php? U= 124362 & C = 7be8ba2b6f3b6cc5