1
public class A
{
    public void doSomething()
    { /*code*/}
}

The doSomething method is in no way referencing the state of object A so by that logic it could be static.

What is the difference between option 1 and 2:

  1. new A().doSomething()
  2. Assuming doSomething is static; A.doSomething()

I want to say that option 2 is better because the first would be creating a new object everytime it is used.

Sababado
  • 2,524
  • 5
  • 33
  • 51
  • 5
    `new A()` creates a new object; calling a static method does not require an instance to be invoked upon. Generally, object creation is *very cheap* and I recommend *interfaces* and *avoiding static methods*. The reasons why won't necessarily become apparent - and at which time you may just thank such suggestions - until using DI and/or IoC. – user2246674 May 03 '13 at 18:43
  • 1
    Maybe it shouldn't even be in class `A`. – Sotirios Delimanolis May 03 '13 at 18:43
  • A.doSomething() would not be able to access A's instance vars they would need to be passed in as params – John Kane May 03 '13 at 18:46
  • 2
    your question was already answered in this thread http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods – Oscar Castiblanco May 03 '13 at 18:48
  • `Static` is best used in cases where the variable/method is needed to retain its value and should be independent of the instance of the class. If `dosumthing()` does not require an instance of `A` to run then I presume it should be `static`. – Sushim Mukul Dutta May 03 '13 at 18:50
  • if you want to mock it then an instance-method is easier to mock (not if you call new A() in your client-code however). – cproinger May 03 '13 at 19:16

4 Answers4

2

Option 1 creates a new instance of A in order to call the method doSomething() which according to your question it sounds like it doesn't need to (there is nothing in doSomething() that requires an instance of A). Option 2 skips the unneeded instance creation while producing the same effect, so it would be better (assuming that this is the only design requirement). Now there might be other reasons to not use static, for instance, if A implemented in interface, or if the nature of doSomething could conceivably change at some point in the future where it might need information established outside of it.

Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
1

You're entering into the "expression" part of programming. What do you want to express?

Three cases are under discussion:

  1. your method is an action any A can take, or a message any given A can respond to,
  2. your method is an action the class of A's should respond to, and
  3. A is a singleton, and your method receives messages for that singleton.

Now ask yourself: what do you intend to express? Is "doSomething" appropriate for the class A? Or is it, rather, just something that every instance of A should be able to do, regardless of its internals? Does "A" represent something which, in the world of your program, should only have 1 instance?

As a practical point, you can't overload static methods, so aside from "expression of intent", you need to be aware of that.

A lot of basic utilities fall in the "static" category, and there's a (small) time penalty for creating a new instance of A, but overall--you're most likely to get it right, and more importantly, the later life of that method will have the least impact on other code, if you can answer the questions above correctly, and thus pick the implementation that matches the intent of the object most closely.

Jerry Andrews
  • 847
  • 5
  • 23
0

There is a third option.

Create one instance of A, then reuse it for each call.

e.g., in the class or Application that is using A,

A myA = new A();  // you can consider making this static if that makes sense...

... then, as needed later on ...

myA.soSomething();

The advantage is that, in the future, if you do need to change the behavior of doSomething, you could change the first line to

A myA = new SubclassOfAThatDoesSomethingDifferent();

Or, if doSomething() eventually does need to reference the state of A, it can.

user949300
  • 15,364
  • 7
  • 35
  • 66
  • I wish this was an option. The reason I ruled this out is because there are a couple of different files/classes that are referencing the same method. – Sababado May 03 '13 at 19:05
-1

You'd have to declare the method as static:

public class A {
    public static void doSomething() {
        // code
    }
}

This allows you to do A.doSomething() and also prevents doSomething() from looking at any instance methods or fields (because how would you know which A instance to use?), which shouldn't be a problem if it doesn't reference them anyway.

See The Java Tutorial's Article on Instance and Class Methods for details.

Leo Izen
  • 4,165
  • 7
  • 37
  • 56
  • I appreciate the response however this wasn't the question asked. I know how to declare a static method and how to reference it in code vs an object creation, my question is more of the behind the scenes difference between the two – Sababado May 03 '13 at 19:02
  • 1
    @Sababado Oh. In that case, I'd recommend using a static method as there's no reason to create an object if it's just going to get GCed immediately. See other answers for more details – Leo Izen May 03 '13 at 19:09