Rethinking Java static keyword

Complete a Java class, although also wrote a lot of Java code, but there have been a lot of doubts, but the static keyword has puzzled me for a long time, no intention to explore how today, Sounds almost carefully checked and found the excellent answer topics https://www.zhihu.com/question/36615154

This article before me completely understand the concept of static collapse, it seems I underestimated the wisdom of the master design language, the old thought it would be a waste of memory space when a non-static function declaration will cause instantiate multiple instances, virtually every the method functions with only inside instance only in the memory area citations instance, this reference point and the head address of the memory function method. After reading the article and then think about the code I wrote before, the people are a little sucker, so-called optimization written, but is not consistent with object-oriented programming specification.

But then I thought about, in object-oriented programming has been popular for 20 years today, why static keyword as well as the meaning of existence it? After thinking a meal time, I found a static function has a very powerful extensions on the object, if used properly, can really optimize memory usage.

For example, take a game For example, suppose we have a qualitative requirements (defined in various non-static properties in the class) of the basic equipment soldiers trained soldiers therefore, there is property of the respective basic equipment (subject to the method), but you can not always write the soldiers with rocket-propelled grenades in the basic properties in aT, m240 heavy machine guns, m320 step grenades, portable radio station. . . If you really like this, to be re-trained soldiers died (instantiate an object of memory for large), results in the task on a knife-wielding thugs, take a pistol chug twice solved, and that is exactly what Bazooka furnishings, a little question masterpiece.

So, a kind of object-oriented the better solution, such as the base class is composed of soldiers and armor + people, then I can differentiate inheritance way into the medic with a rifle, submachine gun + Corps of Engineers with rocket-propelled grenades, machine guns, military supplies + ammunition bags, scout sniper rifle + telescope. So distribution arms, soldiers and equipment just right not too heavy, can take full advantage. But these soldiers are training needs (instantiation, new operation, to give a separate memory space), to resources, a task requires an individual with not two. This case, a special class with a static method will play a role in expansion.

Although static methods when the program starts to take up the memory, and with more static is not a good habit, easily lead to memory overflow. But the demand to re-expand the external static method is a good method. Let's say the ground infantry ground combat pilots call (messaging, a method object using another object, you need to instantiate an object), but also can call the headquarters were hit the ground with a long-range missile (call public class Static the method does not need to instantiate an object with a public class called static method but also have to take up memory, and you want to release at the end of the program). The difference is that, for this military system, the former assignment of resources more aircraft, if less than air strikes, but also waste it. . .

So for some public methods frequently used, and these public methods can be used as extensions, you can have static methods alone write in a class, when other classes need to use when a static method public class individually packaged a method call, extended to form a minimum, save memory resources.

Code examples:

Map encapsulated in a class method, object-oriented programming specification, this method calls the static method KruskalAlgo KruskalMap class, and to pass this static instance Map this function. If you do not want to Kruskal's algorithm, you can also modify the code, static method with Prime class change.

Guess you like

Origin www.cnblogs.com/Jimmy-hacks/p/11614026.html