Static imports vs. definition redirection
According to Java Practices one should usually avoid using static imports.
I get that. I want to know, however, what the difference between the two
practices below is:
Code Sample #1
In the first sample, I statically import SomeClass.someMethod, and use it
within the context of my own class, omitting any name qualifiers.
import static SomeClass.someMethod;
public class MyClass {
public void doSomething() {
someMethod("Hello");
}
}
Code Sample #2
In the second sample, I import the class itself, and then redirect the
call to the method qualified by its full name.
import SomeClass;
public class MyClass {
private void someMethod(String text) {
SomeClass.someMethod(text);
}
public void doSomethind() {
someMethod("Hello");
}
}
What is the essential difference between the two? And I mean beyond the
very obvious advantages proxying a call would give us (e.g. event hooks,
parameter monitoring, etc.) In particular, I want to know if there are any
best practices regarding this, and if this has a performance-related
guideline.
No comments:
Post a Comment