pramodc84
pramodc84

Reputation: 1614

calling method Versus class.method

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();

}

Upvotes: 1

Views: 403

Answers (6)

bl4ckb0l7
bl4ckb0l7

Reputation: 3999

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.

Upvotes: 0

Dennis C
Dennis C

Reputation: 24747

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.

Upvotes: 0

Ken Gentle
Ken Gentle

Reputation: 13357

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.

Upvotes: 12

Jon Skeet
Jon Skeet

Reputation: 1500275

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 :(

Upvotes: 1

Yuval Adam
Yuval Adam

Reputation: 165232

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.

Upvotes: 6

Guillaume
Guillaume

Reputation: 18865

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.

Upvotes: 1

Related Questions