: This article mainly introduces static, when to use, and when to use it. if you are interested in the PHP Tutorial, refer to it. After learning static, the students thought it was very convenient to use static modified members without creating objects. it was troublesome to use members without static modification. they had to create objects before they could be used, however, static is rarely used for defining methods. Why? When should static and static be used?
Aside from the characteristics of static members, let's take a look at the significance of static modification to members.
After a member of a class is modified statically, the member belongs to the class and is shared by all objects. if the member is a member variable, the value of the member and the member is shared by all objects. if the member is a member method, the method is shared by all objects. if the member is not modified using static, each object has its own members. in this case, it can be said that the members of each object are private. Note that this is different from the private permissions in permission control. As you can imagine, the shared method can only access the shared members, but cannot access the private members of the object. the private member method of the object can access the shared members. For example, when we work for the public, we will definitely spend money for the public, but we will never spend money for the private. when we work for ourselves, we can spend money for the public, as long as the public is not pursued. This is why static modifier members can only access static modifier members, such as static modifier member methods, static modifier initialization blocks, and static modifier internal classes. Some people regard static as static or global. In fact, it is similar because the static counterparts are dynamic and the global ones correspond to local ones, depending on which angle you will understand.
In actual development, the reason for defining member variables for a class is that the class has objects of different states. for example, we define a person class and generally define attributes such as age and name, then different people (here is the object of the person class) all have ages and names, but each has its own ages and names, this means that the members of each object mentioned above are private to this object. If the age and name are defined as static, then all people will have the same age and name. Obviously, this is meaningless.
Now, I will explain when to use static to modify the member method.
My personal understanding is very simple, as long as the member method does not need to deal with non-static members, you can use static. In other words, as long as the execution of this method has nothing to do with the state of the object itself, static can be used (this conclusion may be too hasty ). This type of method usually only processes input parameters, and the execution process has nothing to do with its own state, such as the well-known Integer and String.
Integer. toBinaryString (10 );
String. valueOf (12 );
Although both Integer and String have non-static members, both methods do not deal with non-static members, so they can be defined as static.
Generally, when defining a tool class, we usually define its method as static, because this type is only used as a tool and only focuses on its behavior, not its status, so no member variables need to be defined. When using this tool class method, you do not need to create objects, which is simple and resource-saving. Creating an object to call is troublesome and a waste of resources. Therefore, this type of object cannot be created after it is designed, because its constructor is designed as private permission. For example, we use Math, Arrays, and Collections. These three classes are the most common three Tool classes in java.
There are many clever usage of static in actual development. only by understanding the meaning of static can we appreciate its clever usage.
The above introduces static, when to use, and when not to use, including some content, and hope to be helpful to friends who are interested in PHP tutorials.