When to use the static method and field?

I know what is static, but just not sure when to use it.

static variable: I used it only for constant fields. Sometimes a class has dozens of constants, so using static constants can save a ton of memory. Are there other typical use cases?

static method: I use it when I make a class about algorithms. For example, a class that provides various sorting algorithms. Is it against OOP design? I think it is better to support this method, rather than implement sorting algorithms within each class that should use them. Am I mistaken? What are some good use cases?

Also, is there a performance difference between using static and non-static fields / methods?

+4
source share
5 answers

You describe cases when you used static, but that doesn’t quite explain why you use static and non-static - this is more than just keywords for constants and utility methods.

When something is not static (instance), it means that for every instance of the class there is an instance. Everyone can change independently.

When something is static, it means that for all instances of the class there is only one copy of it, so changing it from anywhere affects everyone else.

Static variables / methods usually use less memory because there is only one copy of them, no matter how many class instances you have. Static when used properly, is great for object oriented design.

If you have a method / variable that only needs one instance (for example, a constant or utility method), just make it static. Understand that creating a static method means that you cannot override it. Therefore, if you have a method that you want to override in a subclass, do not make it static.

The general rule is that if you need only one copy of it, make it static. If you need a copy per instance, make it non-static.

+18
source

Are there any other typical use cases?

Global variables

Is it against OOP design?

It is possible that static methods have no status, since you do not need a specific instance of the class. My favorite approach is utilities (like Apache Commons). But you may know that some methods may be better placed as members of a class rather than static.

Static methods can also make checking a class more complex once you cannot override these methods or replace them with a mock implementation.

The difference in performance?

Google’s performance recommendation from Google came up, which says that "prefer static over virtual":

http://developer.android.com/training/articles/perf-tips.html#PreferStatic

I'm not sure if this is true for the JVM since Android uses a different virtual machine, but that makes sense, given the reasons the link points to:

If you do not need to access the fields of an object, make your method static. Calls will be approximately 15-20% faster. This is also good practice, because you can tell by the method signature that a method call cannot change the state of an object. "

+3
source

Static variables belong to the class, so they are shared by all objects, so the memory usage is less if you really want varible to be shared. If you declare a variable as public and static, then it is accessible to everyone.

Static methods are usually utility methods, depending on the access modifier, which can be used within a class or between classes. The static utility class will help reduce memory usage again, because you do not need to create an object to call these methods.

0
source

My personal rule of thumb is that static things "just hang there." These are things that (disclaimer, not quite true) are global, but it makes sense to include in this particular class.

Static fields are good if you are loading a few heavy objects again. For example, the project I'm working on now has a switch between two images. These are static fields that are loaded into the application and stored in memory, rather than reloading them every time and allowing the GC to take care of the mess.

0
source

Besides very specific situations, I use only static (and final) variables for constants only. Of course, this is quite realistic to use.

I try to avoid static methods of the utility because they make it difficult to write unit tests for code (mocking the results of a method call). When you start developing Test Driven, this problem becomes apparent. I prefer to use dependency injection and singleton beans (although this depends on your needs and situation).

0
source

Source: https://habr.com/ru/post/1491171/


All Articles