0

I had a discussion about usage of static method, briefly the argument is should a class definition have a method as static or instance method in the following scenario. There is a class that defines an entity i.e, what its properties are and what operations are allowed on it.

class dummy{
    String name;
    String content;

    String someRandomOpeation(){
       ....
    }

    static String createKey( String inputName, String inputContent ){
       return inputName+inputContent;
    }
}

The class definition also has a static method that takes in some arguments (say content and date, which defines an instance logically) and use it to construct a key (a string) and return the same. Now if an instance of the message is created it would have the content and date as fields. Is the argument that I can get a key given a name and content and not have to create an instance valid to have the static method. Or does the fact that a pair of name and content logically define an instance say that an instance to be created and get a key from that?

broun
  • 2,483
  • 5
  • 40
  • 55
  • See: http://stackoverflow.com/questions/538870/java-static-methods-best-practices – dogbane May 14 '14 at 07:50
  • dogbane - Thanks for the response, but the case here is slightly diff where the static methods are public and the args can define an instance logically. – broun May 14 '14 at 07:55
  • Based on the comments, it seems that the question rather aims at the method being `static String createKey(String, String)` or `static String createKey(Dummy)` - with `String createKey()` (an instance method) being the third option. – Marco13 May 14 '14 at 08:33

3 Answers3

0

Having a method as static just because you do not wish to instantiate is NOT a valid argument. According to design we use static methods in helper/util classes which may or may not have any properties of its own. These classes basically help in performing some common action which many different classes can use. Also the functions are so modular that they only use the arguments passed to the function to derive the output.

The class mentioned by you will not work because you cannot make a static reference to a non static field constant in JAVA.

Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
  • the class does make a static reference to non static fields, the static method takes in 2 args and acts on that. it never uses the instance fields. I have edited the names for clarity. – broun May 14 '14 at 08:01
0

Moreover, the disadvantage of using static methods strictly associated with its class fields is that you can not override them.

Szarpul
  • 1,531
  • 11
  • 21
0

Is the argument that I can get a key given a name and content and not have to create an instance valid to have the static method.

No. In this case you almost do not need a class at all. You can always pass data members to class methods, but that does not mean that we do not need classes.

There is an additional argument - encapsulation. It is much clearer and niced to have an instance method getKey() with no arguments and hide the implementation details.

Or does the fact that a pair of name and content logically define an instance say that an instance to be created and get a key from that?

Also no. The fact itself does not mean that you should create an instance.

The first question here is wether we need a class or not. Do we really have some meaningfull objects, instances of something (dummy)? For example, if I have a library, it obviously make sense to have a class Book, as I have lots of books around. Do you have such a situation? Is this key logically associated with an instance of whatever (dummy here), in a way that Author and ISBN are logically associated with a Book? Does instances of this concept (dummy) has some relationships with some other concepts? Like a Book can be rented. If most of these answers is yes - than this method should be instance method with no arguments.

Or... Do you maybe only need kind of a "service" that calculates a key from 2 strings? It this all you need? Are there some other similar "services" that you need, independent on instances. If these answers are mostly "yes", that you do not even need a class. Or you can have a class "Services", with no attributes and with this static method and arguments (and maybe additional methods).

There is also a mixed situation. You still need instances for some other purpose, but you also need a createKey method that is totally independent on any instance, and only provide some global service that is somehow related with the class logic. Then a static method can make sense.

I think your way of thinking is kind of function-oriented instead of object oriented. I think you need a "click" in your mind, which would help you understand the right purpose and the meaning of objects and classes.

Aleks
  • 5,674
  • 1
  • 28
  • 54