After learning the static , the students feel that the members with static decoration is particularly convenient, without creating objects can be used without static The decorated members appear troublesome and must be created to use, but we seldom use static when we define the method . Why, when to use static, and when not static ?
let's throw it away . Static members to use the characteristics of understanding Static the meaning of the decorated member.
A member of the
class is static decorated, this member belongs to the member of the class, is shared by all objects, If the member is a member variable, the member and the value of that member are shared by all objects, and if the member is a member method, the method is shared by all objects, and conversely if the member is not using decoration, then each object has its own members, it can be said that the members of each object is his private, note that this and permissions control in the private permissions are two different. As you can imagine, a shared method can only access shared members, cannot access the private members of the object, and the private member method of the object can access the shared member. For example, just as we work for the public, it must cost the public money, never spend private money, but to work for themselves, but can spend the public money, as long as the public is not pursued. This is why static decorated members can only access Static decorated members, such as static decorated member methods, Span lang= "en-us" > static decorated initialization block also has static This is true for the decorated inner classes. Also some people think of static as Static or global, in fact almost, because static counterpart dynamic, global corresponding to local, See from which angle you understand.
in the actual development, the reason for the class to define member variables, is because the class has different state of the object, such as we define people this class, the general will define the age, name and other attributes, and then different people (here is the object of this category) although all have age and name, but each has the age, each has its own name, That's what it says. The members of each object are private to that object. If you define age and name as static , it will cause all people to have the same age and name, which is obviously meaningless.
now answer when to use Static modifies the member method.
My personal understanding is simple, as long as the member method does not need to deal with non- static members, you can use static. In other words, you can use static as long as the execution of the method is not related to the state of the object itself (This conclusion may be too hasty). Such methods usually only deal with incoming parameters, and the execution process has nothing to do with its own state, such as the integers and stringsWe know well.
Integer. tobinarystring (ten);
String. valueOf (n);
Integer and the String Although all have non- Static But neither of these methods is associated with a non- Static members, so they can be defined as Static the.
in general, when defining a tool class, we generally define its method as Static , because this class is used only as a tool, focusing only on his behavior and not on his state, so there is no need to define member variables. The method of using this tool class eliminates the need to create objects, which is both simple and resource-saving. Creating objects to invoke is cumbersome and wasteful, so this kind is designed to simply not allow objects to be created, because their construction methods are designed to be private permissions. Like the Math and ArraysWe used, and the collections. These three classes are the most common in our Java three tool classes.
Static in the actual development of a lot of clever use, only to understand the meaning of static , we can appreciate the ingenious.
The above describes the static, when to use, when not, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.