views:

404

answers:

9

Hello,

I am wondering when to use static methods? Say If i have a class with a few getters and setters, a method or two, and i want those methods only to be invokable on an instance object of the class. Does this mean i should use a static method?

e.g

Obj x = new Obj();
x.someMethod

or

Obj.someMethod

(is this the static way?)

I'm rather confused!

A: 

If you want an instance to be able to call a method, it should not be static.

Static methods can be called from anywhere in your program.

For Reference: http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

webdestroya
Your first sentence is misleading. It is perfectly ok for an instance method to invoke a static one.
meriton
Only by referencing it via the class (I think). If you did "this.staticMethod()" I think it would error.
webdestroya
I agree, it is misleading. You could still `staticMethod()` directly. I think it should be: If you want to invoke a method on an object and be able to access it's instance variables/methods, it need not be static.
msakr
Well, have you actually fed "this.staticMethod()" to a compiler before posting this? You might be surprised, because the Java Language Specification allows a static method to be called like an instance method. Granted, such usage is discouraged, but perfectly legal. It would have been polite to clarify (edit) your misleading sentence. Since you did not, and instead posted more misleading stuff: -1.
meriton
I said, "should not" in my original post, and not "can not". I do agree that my comment was wrong.
webdestroya
A: 

Static methods in java belong to the class (not an instance of it). They use no instance variables and will usually take input from the parameters, perform actions on it, then return some result. Instances methods are associated with objects and, as the name implies, can use instance variables.

Kevin Sylvestre
+1  A: 

No, static methods aren't associated with an instance; they belong to the class. Static methods are your second example; instance methods are the first.

duffymo
+4  A: 

Use a static method when you want to be able to access the method without an instance of the class.

Jamey
A: 

Static: Obj.someMethod

Use static when you want to provide class level access to a method, i.e. where the method should be callable without an instance of the class.

Finbarr
A: 

Static methods are not associated with an instance, so they can not access any non-static fields in the class.

You would use a static method if the method does not use any fields (or only static fields) of a class.

If any non-static fields of a class are used you must use a non-static method.

Carsten
+3  A: 

One rule-of-thumb: ask yourself "does it make sense to call this method, even if no Obj has been constructed yet?" If so, it should definitely be static.

So in a class Car you might have a method double convertMpgToKpl(double mpg) which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But void setMileage(double mpg) (which sets the efficiency of one particular Car) can't be static since it's inconceivable to call the method before any Car has been constructed.

(Btw, the converse isn't always true: you might sometimes have a method which involves two Car objects, and still want it to be static. E.g. Car theMoreEfficientOf( Car c1, Car c2 ). Although this could be converted to a non-static version, some would argue that since there isn't a "privileged" choice of which Car is more important, you shouldn't force a caller to choose one Car as the object you'll invoke the method on. This situation accounts for a fairly small fraction of all static methods, though.)

not-just-yeti
+2  A: 

After reading Misko's articles I believe that static methods are bad from a testing point of view. You should have factories instead(maybe using a dependency injection tool like Guice).

how do I ensure that I only have one of something

only have one of something The problem of “how do I ensure that I only have one of something” is nicely sidestepped. You instantiate only a single ApplicationFactory in your main, and as a result, you only instantiate a single instance of all of your singletons.

The basic issue with static methods is they are procedural code

The basic issue with static methods is they are procedural code. I have no idea how to unit-test procedural code. Unit-testing assumes that I can instantiate a piece of my application in isolation. During the instantiation I wire the dependencies with mocks/friendlies which replace the real dependencies. With procedural programing there is nothing to "wire" since there are no objects, the code and data are separate.

Alfred
A: 

Static methods don't need to be invoked on the object and that is when you use it. Example: your Main() is a static and you don't create an object to call it.

Vaishak Suresh