3

As a general convention, should static method(s) be separated into another class from a class with instance methods?

Is there also an example of your reason?

Razib
  • 10,965
  • 11
  • 53
  • 80
Beginner questions
  • 343
  • 1
  • 4
  • 15

5 Answers5

6

There is no general convention that dictates that a static method must be separate from a non-static method. In fact, if the two methods are related enough to one another, it would be counter-intuitive to have the methods separated.

Recall what use case static methods (and fields) have: they're methods/fields that can be used without an instance of a particular class. This generally means that they hold valuable metadata or perform a useful operation that's related to their class instance, but would not require direct instantiation of that class.

Take, for example, Integer. It has the static [final] fields MAX_VALUE and MIN_VALUE. Since both of these fields contain fixed information that would not change between instantiations, it would not make sense to have to instantiate an Integer to get this information.

Integer also has the useful operation parseInt, which takes a String and turns it into an int. We shouldn't require an instance of Integer to convert from String to int, especially if we're not placing it into an instance of Integer.

The overarching convention has been to keep related methods together, regardless of if they're static or not. You can see clearer examples of this in certain Java library classes, like Integer.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Just wondering on your point there, why would you not want to create an instance of a Int to convert the string to an int? – Beginner questions Apr 25 '15 at 19:02
  • You don't have an instance of an `Integer` to work with. The idea is that this is a [factory](http://en.wikipedia.org/wiki/Factory_method_pattern), or something that can continually produce `int` values. – Makoto Apr 25 '15 at 19:04
  • couldnt something like IntegerStatic.parseInt() work without having to create an int though? (im not trying to make out this exists just a possible idea to hopefully show my confusion) – Beginner questions Apr 25 '15 at 19:12
  • I think the bigger confusion here is the difference between `int` and `Integer` in this example; they're *not* the same thing. In the scenario you mention, the only place it'd make practical sense to not use a static method and create a new instance of an `Integer` is via the object constructor. – Makoto Apr 25 '15 at 19:15
  • sorry i meant Integer – Beginner questions Apr 25 '15 at 19:16
2

It probably is a duplicate question, but no, static methods have very specific benefits that are often valuable in classes that are instantiated as objects.

Brian Topping
  • 3,235
  • 28
  • 33
2

There is no such convention. It's completely depends on your situation. Some class may really needs mixture of both static and non-static members.

But some times it's is seen the use of Constatns.java/ Utils.java class in some java project. You may found -

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 these class to provide some constants or utility method all over the project.

Razib
  • 10,965
  • 11
  • 53
  • 80
2

Definitely answer would be dictated by the use case but there is no convention as such. At most you have some Utility class that may have bunch of static methods that are used by other classes as helper methods. For example to test whether a String is an email or to extract username from email etc.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
2

Putting all static methods in a separate class would be useful while writing an API or a framework. Collections class is an example. java.lang.Math or java.lang.System is another.

Normally, define static methods in the following scenarios:

  • While writing utility classes .
  • If the method does not use any instance variable.
  • If any operation is in-dependent of instance creation.
  • If you are sure that the definition of the method will never be changed or overridden. As static methods can not be overridden.

see here - https://stackoverflow.com/a/5313383/760393

Community
  • 1
  • 1
Raúl
  • 1,542
  • 4
  • 24
  • 37