Call 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 is the best fit? Or could this be a question?
public static method2() {
method1();
}
OR
public static method2() {
Routines.method1();
}
source to share
While I agree with existing answers that this is primarily a style issue, there is enough of a style issue that Eclipse and IntelliJ code critics will specify "non-static static method references" in code that does not use Classname.method()
style.
I made it a habit to underline intent, using the class name 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 this is less important these days. I like it for the maintainer (often myself) to know what was intended, yes I knew it was a link static
.
Yes it does a bit more verbose code, but I think it's worth the extra characters.
source to share
I would go with the first method. In my eyes, this is equivalent to:
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 explicitly call ClassName.
source to share
Well this is relevant to the question, but obviously the results will be the same anyway, so it's just a matter of style. (There are probably weird overload situations where this can make a difference, but you should avoid where to start. I can think of examples if you want, but it probably isn't worth it.)
If you feel a special need to emphasize that this is a static method, feel free to make it Routines.method1()
, but usually I just leave it as method1()
.
EDIT: I tried to come up with an example where it matters 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. (Interestingly, the static method params
gives a slightly confusing error message using the MS C # compiler and completely kills the Mono compiler - at least the version I'm using.)
With a parameter, you can run into odd situations with type parameters and type inference, but not without any parameters. However, a non-generic type will take precedence.
In short, I don't think I can do it in the end :(
source to share
This is only a matter of personal utility and code readability, but calling only method1 () is not equivalent to "this.method" because both methods in the example are static and you cannot call an object that has not been created. With static methods, you cannot anticipate if there is an instance of an object at the time the method is called, so you cannot use "this". "this" is only a pointer to the executable, if created.
To answer the question:
It fills in syntactic sugar to just write "method1 ()" instead of "Routines.method1 ()". The result of what the computer does / calls, which is a compiler made from code, is exactly the same.
source to share