Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why would there be an advantage to use a static method and pass the reference to an object as a parameter rather than calling the method on an object?

A clarification on what I mean may be found in the following class:

public class SomeClass {

    private double someValue;

    public SomeClass() {
        // Some constructor in which someValue is set
    }

    // Called on an instance
    public void incrementValue() {
        someValue++;
    }

    // Static method with parameter pass
    public static void incrementValue(SomeClass obj) {
        obj.someValue++;
    }

}

My question is not restricted to this class alone; any point where you'd pass an object instead of calling it on a method is what I'm interested in. Is this ever advantageous? If so, why?

share|improve this question
1  
It feels like a code smell with two methods doing the exact same thing. If the static method simply delegated to the other method, then it would feel useless, but not necessarily "bad" – Nathan Merrill 22 hours ago
13  
@NathanMerrill I think you're missing the point. He's asking if there's ever a situation where creating and using the second method instead of the first method would be preferable. – Mego 22 hours ago
    
@Mego Not only the methods given in the example; I'm asking if there is any moment when using static methods and passing objects is better than calling methods on objects? – VoteToClose 22 hours ago
    
Presumably you are asking specifically for java? – enderland 22 hours ago
1  
I think this question makes a tacit assumption that object oriented code is some kind of optimum. A more procedural or functional approach would naturally lead to the use of static methods. ...Duplicating the functionality between a static and a instance method is pretty silly, though. I hope that's just an example and the actual code you're talking about only has the static. – jpmc26 11 hours ago

A trivial example: when the instance passed can legitimately be null and you want to incorporate the (non-trivial) handling of this into the method.

share|improve this answer
1  
example: String.Compare in C# (I think there is something similar in Java) – edc65 15 hours ago

In your example, the instance method is a clear winner.

In the general case, I can think of a few reasons where a static method might be appropriate:

  • You want to put the static method in another class, since you have a situation where it makes sense to separate the logic from the data (note: your example is not one of them).

  • You are passing two or more objects and want to emphasize that they are of equal importance.

  • null is a valid value (as explained by user 9000).

share|improve this answer

A method can only be made static if it doesn't access any of the non-static members. Passing the object to a static method and make changes in the object is as stupid as walking on your hands. We can do it, but is it what hands are meant for?

But we can find examples of static methods which are pure methods and take the object as input, like when we need to instantiate the object based on certain validation rules. For example, .NET has the method DateTime.TryParse(String s, DateTime d) to validate and instantiate the object. But the parameter DateTime d is explicitly marked as out.

Another case can be when we compare the objects and want to get the desired object as return value rather than a boolean / integer value of comparison result, for example, Team.GetHigherScorer(teamA, teamB).IncreaseRanking(). This will be cleaner than:

int compareResult = teamA.compareScoreWith(teamB);
if (compareResult == 1)
    teamA.IncreaseRanking();
else if (compareResult == -1) 
    teamB.IncreaseRanking();

(leaving the case "draw" out for simplicity).

share|improve this answer
1  
One does not pass an "entire object". You pass the reference to a place in memory, which is a very small amount of data being passed. I'm not sure about the affect on performance that that has, but nevertheless... also, what does the "out" marking imply? – VoteToClose 21 hours ago
1  
I dropped the word 'entire' from my statement, it was confusing. I never intended performance or size, the answer is purely based on programming practices. Anyway, out is a .Net keyword used as a parameter modifier. It states that the parameter is passed by reference. See for details msdn.microsoft.com/en-us/library/t3c3bfhx.aspx – wonderbell 20 hours ago

Dependency Injection would be a good reason to perform the call to the static method. Assuming that the concrete implementation of SomeClass has an inheritance chain or is the implementation of another class. You could use a mock of an object, pass that it for testing purposes to insure that your method does what it is supposed to, and then reports on that status.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.