views:

301

answers:

6

I have a class with two methods defined in it.

public class Routines {

     public static method1() {
      /* set of statements */
     }

     public static method2() {
      /* another set of statements.*/
     }
}

Now I need to call method1() from method2()

Which one the following approaches is better? Or is this qualify as a question?

public static method2() {

        method1();

}

OR

public static method2() {

        Routines.method1();

}
A: 

Well, it qualifies as a question, but obviously the results are going to be the same either way so it's just a matter of style. (There are probably weird overload situations where it could make a difference, but you should avoid those to start with. I can come up with examples if you want, but it's probably not worth it.)

If you feel a particular need to emphasize that it's a static method, feel free to make it Routines.method1() - but normally I'd just leave it as method1().

EDIT: I've tried to come up with an example where it makes a difference using overloading with params:

void CallMethod()
{
    Console.WriteLine("Calling Method()");
    Method();
    Console.WriteLine("Calling Test.Method()");
    Test.Method();
}

void Method(params string[] ignored)
{
    Console.WriteLine ("  Instance method called");
}

static void Method()
{
    Console.WriteLine ("  Static method called");
}

This calls the static method in both cases though. (Interestingly, putting the params on the static method gives a slightly confusing error message using the MS C# compiler, and blows up the Mono compiler completely - at least the version I'm using.)

With a parameter, you could get into odd situations with generic type parameters and type inference, but not without any parameters. Even so, the non-generic form would take precedence.

In short, I don't think I can do it after all :(

Jon Skeet
static methods can't be overridden so I think this is purely a style question
basszero
I didn't mention overriding. I mentioned overloading.
Jon Skeet
Jon: I would love to see an example :)
Eyvind
In Java (not sure about C#), overloaded static methods only get tricky when you're extending the parent class and creating new methods w/ matching signatures. Fully qualified method invocation avoids the problem all together.
basszero
@basszero: The tricky thing in my *attempt* was that the two methods have different signatures, but they both match. However, this is already unambiguous in the spec, by the looks of it.
Jon Skeet
+1  A: 

This is purely a style question, so it depends on your taste.

I prefer the first version. As the 2 methods are in the same class I dont find useful to repeat the class name.

Guillaume
+1 for "purely style"
matt b
+6  A: 

I'd go with the first method. In my eyes, it's the equivalent of:

public void method2()
{
    method1();
}

and:

public void method2()
{
    this.method1();
}

I don't know many people who explicitly call this when calling another method in a class. So personally my taste is option 1 - no need to call ClassName explicitly.

Yuval A
Just don't use this.method1() if both methods are static. While it works, you'll get a compiler warning.
R. Bemrose
@R. Bemrose - it won't work, you'll get a compiler *error*. You can't call a static method with *this*.
Yuval A
You can't use "this" from a static method--that is an error. You can use this.staticMethod() from a non-static method. That will generate a warning by default--some compilers can be configured to treat this as an error.
James Schek
+10  A: 

While I agree with the existing answers that this is primarily a style issue, it is enough of a style issue that both Eclipse and IntelliJ's code critics will flag "non-static references to static methods" in code that does not use the Classname.method() style.

I made it a habit to emphasize intent by using the classname to qualify references to static targets, this to qualify references to instance targets, and bare names for local references. A modern IDE will use different highlighting for these constructs, so I suppose it is less important these days. I like for the maintainer (often myself) to know what was intended, that yes, I knew that was a static reference.

Yeah, it does make for slightly more verbose code, but I think it is worth the extra characters.

Ken Gentle
is not the warning when using static member in non-static context ? here the method2() is a static context... so I think there won't be a warning...
Vinze
@Vinze: I've seen that message, too. You're most likely correct that in this specific instance, that message wouldn't be shown. However, as the question was about style and why, I generalized a bit (and perhaps didn't read the example closely enough ;-) )
Ken Gentle
wholeheartedly agree, and I use "this." as well - clarity is everything
annakata
This (pun intended :)) is a matter of style, and has been discussed at length on many questions already...
Yuval A
Do you also use the Classname when referring to a constant? i.e. Classname.MY_PUBLIC_FINAL_VALUE and Classname.MY_PRIVATE_FINAL_VALUE from within Classname?
James Schek
Yes, very often I do in the "public" case. In the private case, not always because the naming convention (ALL_UPPER) conveys the intent. The introduction of static imports in Java has had me reconsidering this practice for public constants/methods, but to date, I'm still following what I described.
Ken Gentle
A: 

I do

public static method2() {
        Routines.method1();
}

No guessing. It is very clear if I call static method from parent class. I don't like static import for same reason.

Dennis Cheung
Agree. I don't even think it's a style question. The correct thing to do is to make your code clear, and this demonstrates that it's a static method.
mtruesdell
I bet we will have less bugs in all programs, if Java/C# compiler force us to add "this." / "Class." for all class member; everything else are local.
Dennis Cheung
A: 

It's only an issue of personal favour and code readability, but calling just "method1()" is not an equivalent to "this.method", because both methods in the example are static and you cannot call an object, which has not been instantiated. With static methods you cannot foresee, if there is an instantiated object the moment you call the method, so you cannot use "this". "this" is only a pointer to the executed object, if instantiated.

To answer the question:

It's complete syntactic sugar to just write "method1()" instead of "Routines.method1()". The result on what the computer is executing/calling, which is what the compiler made out of the code, is completely the same.

bitschnau