Rethinking the Java static keyword

Time:2019-10-24

The Java class, while also wrote a lot of Java code, but there has been a lot of doubt, the static keyword has puzzled me for a long time, today didn’t mean to study, on zhihu check again carefully and found the best answer to the topic of https://www.zhihu.com/question/36615154

This article thoroughly understanding of the static concept crumble before me, I still seem to underestimate the design language of the master’s wisdom, the old thought non-static statement function will cause the instantiation multiple instances will be a waste of memory space, in fact in each instance function method in the instance in the area of memory just only a reference, and the reference is to the memory address of the function method. After reading the article and thinking about the code I wrote before, I was a little silly, writing the so-called optimization, but not in accordance with the specification of object-oriented programming.

But then I thought about why the static keyword still exists today when object-oriented programming has been popular for more than 20 years. After a meal of thinking, I found that static functions have a very powerful function in object extension, and when used properly, can really optimize the memory footprint.

Take, for example, a game for example, suppose that we are equipped with basic qualitative requirements for soldiers (all kinds of static attributes defined in the class), so trained soldiers, all have their own basic equipment (object’s properties and methods), but you can’t always write in basic attribute soldiers AT rocket launchers, m240 heavy machine guns, m320 rifle grenades, portable radio base station… If this is true, the trained soldier will die again (instantiating the object takes up a lot of memory), and the result is a knife wielding gangster who can solve the problem with two quick blows with a pistol.

Therefore, there is a better solution in object-oriented, for example, the basic class is armor + man soldier, then I can divide it into medic with rifle, engineer with submachine gun + bazooka, resupply machine gun + ammunition bag, scout sniper rifle + telescope by inheritance. In this way, the soldiers are properly equipped without being overweight and can make full use of it. But these soldiers need to be trained (instantiated, new, given a separate piece of memory space), resources, and never two for each task. In this case, a special class with static methods is an extension.

Even though the static method occupies the memory when the program starts, it is not a good habit to use static method too much, which can easily lead to memory overflow. But it is a good idea to re-extend external static methods as needed. Say a ground attack to ground infantry call pilot (messaging, an object using another method of object, need to instantiate objects), but can also call headquarters, with a ground attack to long-range missile (call a static method in a public class, object and should not be instantiated with public class calls, but the static method also takes up memory, and at the end of the program to release). The difference is, for this military system, the resources sent to the former more planes, if not air strikes, waste oil…

Therefore, for some commonly used public methods that can be used as extensions, they can be written in a single class as static methods. When other classes need to use them, they can encapsulate a method to call the static method of the public class, so as to form a minimum extension and save memory resources.

Code examples:

Encapsulating a method in the Map class that implements the object-oriented programming specification calls the static method KruskalAlgo in the KruskalMap class and passes the Map instance into the static function. If you don’t want to use Kruskal, you can change the code to the static method of the Prime class. Note that static methods reside in memory and are freed only when the JVM is closed, so only functions that are frequently called are extended.

Static methods perform more efficiently than non-static methods because the address in the heap is continuous and the non-static is discrete, and the function used requests memory.

The other is for the rarely used function, the function of this class is non-static, use the instance, and release the memory of the method after use, which is slower than the direct object-oriented writing method, because it involves new:


Test cases:

Test results: