Aamir
Aamir

Reputation: 2424

static member in Java

package pack1;

public class A {
    public static int i = 10;

    public static void test()  {
    System.out.println("done");
    }

 }



//this is other .class file
package pack2;
import static pack1.A;  
import static java.lang.System.out;
public class Manager2 {
  public static void main(String[] args) {
           out.println(i);
           test();
  }
}

Whenever I use import static pack1.A; as I have used in class Manager2 instead of import static pack1.A.*;, the compiler shows an error. Doesn't import static pack1.A; import class A including static members?

I'm aware of that using import static pack1.A.i imports static members, but 'import static pack1.A;' is showing an error.

Upvotes: 0

Views: 245

Answers (1)

Edd
Edd

Reputation: 3822

The Java Language Specification only describes two different approaches to static imports.

Single-Static-Import declarations of the form import static TypeName . Identifier ; which "imports all accessible static members with a given simple name from a type".

The Static-Import-on-Demand declaration allows "all accessible static members of a named type to be imported as needed", and is of the form import static TypeName . * ;.

Essentially you have to specify all the method names that you wanted imported, or use the .* notation if you want to be able to use any method as required.

Upvotes: 1

Related Questions