like this:
import static com.showboy.Myclass;
public class Anotherclass{}
And what's the difference between "import static com.showboy.Myclass" and "import com.showboy.Myclass"?
like this:
import static com.showboy.Myclass;
public class Anotherclass{}
And what's the difference between "import static com.showboy.Myclass" and "import com.showboy.Myclass"?
See http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html
The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.
So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes. If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.
Static import is used to import static fields / method of a class instead of:
package test;
import org.example.Foo;
class A {
B b = Foo.B_INSTANCE;
}
You can write :
package test;
import static org.example.Foo.B_INSTANCE;
class A {
B b = B_INSTANCE;
}
It is useful if you are often used a constant from another class in your code and if the static import is not ambiguous.
Btw, in your example "import static org.example.Myclass;" won't work : import is for class, import static is for static members of a class.
There is no difference between those two imports you state. You can, however, use the sttic import to allow unqualified access to static members of other classes. Where where I used to have to do this:
import org.apache.commons.lang.StringUtils;
.
.
.
if (StringUtils.isBlank(aString)) {
.
.
.
I can do this:
import static org.apache.commons.lang.StringUtils.isBlank;
.
.
.
if (isBlank(aString)) {
.
.
.
Кто вы такие? Я вас не звал! Идите нахуй!
import pkj1.pkj2.*;
import stativ pkj1.pkj2.Xyu.avgLength
the difference between "import static com.showboy.Myclass" and "import com.showboy.Myclass"?
The first should generate a compiler error since the static import only works for importing fields or member types. (assuming MyClass is not an inner class or member from showboy)
I think you meant
import static com.showboy.MyClass.*;
which makes all static fields and members from MyClass available in the actual compilation unit without having to qualify them... as explained above