Reputation:
//interface1.java
package package1;
public interface interface1 {
static final int a =10;
}
//StaticImportTest.java
import static package1.*; //import package1.*; works
class StaticImportTest {
public static void main(String args[]) {
System.out.println(a); //System.out.println(interface1.a) works
}
}
when i am replacing the word "import static" with only "import" and using System.out.println(interface1.a) it works, but not sure why its not working in its current form.
Upvotes: 2
Views: 747
Reputation: 991
For your static import to work the way you intended it would have to be
import static package1.interface1.*
or import static package1.interface1.a
A static import imports public static members of a class either all with * or a specific one like for example a
.
A import on the other hand imports a package or specific classes from a package.
Your import static package1.*
would try to import all members from the class package1
in the root package.
Making it a normal import and accessing a
via interface1.a
works because the import imports all classes from the package1
including interface1
, therefor you can access a
via the interface1
class.
Upvotes: 1