Should static methods be decoupled from classes using instance methods?

As a general convention, should a static method be split into another class from a class with instance methods?

Is there also an example of your reason?

+3


source to share


5 answers


There is no general convention that a static method should be separated from a non-static method. In fact, if the two methods are sufficiently interconnected with each other, it would be consistent to separate the methods.

Remember what static methods (and fields) are used to be used: they are methods / fields that can be used without an instance of a particular class. This usually means that they contain valuable metadata or perform a useful operation associated with their class instance, but do not require direct instantiation of that class.

Take, for example Integer

. It has static fields [ final

] MAX_VALUE

and MIN_VALUE

. Since both of these fields contain fixed information that would not change between instances, it would be pointless to instantiate Integer

this information.



Integer

also has a useful operation parseInt

that takes String

and turns it into int

. We don't need to require an instance Integer

to convert from String

to int

, especially if we don't put it into an instance Integer

.

The overarching convention was to use related methods together , whether they are static or not. You can see clearer examples of this in some classes of the Java library, for example Integer

.

+5


source


This is probably a recurring question, but no, static methods have very specific advantages that are often valuable in classes that are instantiated as objects.



+2


source


There is no such agreement. It totally depends on your situation. Some class may really need a mixture of both static and non-static members.

But sometimes in the class of a java project, using the class is considered Constatns.java/ Utils.java

. You can find -

public static final double PI = 3.1416;
public static getArea(double r){}

      

This class contains some final static property and some final method. The purpose of this class is to provide some constants or utility method throughout the project.

+2


source


A definite answer will be dictated by precedent, but there is no such agreement. At best, you have a class Utility

that can have a bunch of static methods that are used by other classes as helper methods. For example, to check if String is an email or to extract username from email, etc.

+2


source


Putting all static methods in a separate class would be helpful when writing an API or framework. Collections is an example. java.lang.Math or java.lang.System is different.

Typically set static methods in the following scenarios:

  • When writing utility classes.
  • If the method does not use an instance variable.
  • If any operation does not depend on instantiation.
  • If you are sure that the method definition will never be changed or canceled. Since static methods cannot be overridden.

see here - fooobar.com/questions/13195 / ...

+1


source







All Articles