Home  >  Article  >  Backend Development  >  static, when to use it, when not to use it

static, when to use it, when not to use it

WBOY
WBOYOriginal
2016-07-30 13:30:322011browse

After learning static, the students all felt that it was particularly convenient to use members modified with static, which can be used without creating objects, instead of using staticmodification The members are troublesome. You have to create an object to use them, but we rarely use them when defining methodsstatic. Why, when to use static and when not to use static?

Let’s put aside the characteristics of static members and understand the meaning of static modifying members.

After a member of the class is modified by static, the member becomes a member of the class and is shared by all objects. If the member is a member variable, then the member and its value are Shared by all objects; if the member is a member method, then the method is shared by all objects; on the contrary, if the member is not modified with static, then each object has its own member. At this time, it can be said, The members of each object are private to it. Note that this is different from the private permissions in permission control. As you can imagine, shared methods can only access shared members and cannot access private members of the object, while private member methods of the object can access shared members. For example, when we work for the public, we will definitely spend public money, and we will never spend private money. But when we work for ourselves, we can spend public money, as long as the public does not hold us accountable. This is why staticmodified members can only access staticmodified members, such as staticmodified member methods, staticmodified This is true for initialization blocks and internal classes modified by static. Some people also understand static as static or global. In fact, they are almost the same, because static corresponds to dynamic, and global corresponds to local. It depends on which perspective you understand it from.

In actual development, the reason why member variables are defined for a class is because the class has objects with different states. For example, when we define the class of people, we usually define attributes such as age, name, etc., and then different people ( Here are objects of the human class) Although they all have ages and names, each has its own age and each has its own name. This means that the members of each object mentioned above are private to the object. If age and name are defined as static, it will lead to all people having the same age and name, which obviously makes no sense.

Now answer when to use static to modify member methods.

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 the method has nothing to do with the state of the object itself, you can use static (this conclusion may be too hasty). This type of method usually only processes the incoming 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);

String

Although there is something wrong Members of static, but neither of these methods deal with members of non-static, so they can be defined as static.

Usually, when we define a tool class, we usually define its method as static, because this kind of class is only used as a tool and only pays attention to its behavior and not its status, so No need to define member variables. There is no need to create objects when using this tool class method, which is simple and saves resources. Creating objects to call is troublesome and wastes resources, so after this class was designed, it was simply not allowed to create objects, because its construction method was designed to have private permissions. For example, we use Math and Arrays, as well as Collections. These three classes are the three most common tool classes in our java.

static has many clever uses in actual development. Only by understanding the meaning of static can we appreciate the ingenuity.

The above has introduced static, when to use it, and when not to use it, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn