Learned today and applied to static methods in Java, and learned about its advantages and disadvantages.
life cycle (Lifecycle):
Static methods , like static member variables, belong to the class itself and are loaded into memory when the class is loaded, not automatically destroyed, and will persist in memory until the JVM shuts down.
non-static method ( Non-static method), also known as an instance, is an instance object that allocates memory after instantiation and must be referenced by an instance of the class. Memory is not resident, and then disappears after the instance object is reclaimed by the JVM.
storing locations in memory
static methods and static variables always use the same block of memory after they are created, and are contiguous.
non-static methods exist in multiple places of memory and are discrete.
Efficiency
The efficiency of static methods is higher than that of non-static methods.
Thread Safety
A static method is a shared code segment, and a static variable is a shared data segment. Since it is "shared," there is an issue of concurrency (concurrence).
non-static methods are targeted at an object that is determined, so there is no thread-safe issue.
Scope of Use
static methods : ⒈ classes that have static properties, typically define static methods. ⒉ a class that has no attributes, a static method is typically defined. ⒊ If a method is not related to an instance object of his class, then it should be static. static methods can be inherited but cannot be overwritten .
Total
If a static method is defined too much in the system, it consumes a lot of resources and eventually causes memory overflow, so static methods cannot be abused. If you look at thread safety, performance, and compatibility, it is advisable to choose an instantiation method.
static method in Java