In Java- “Static Members of the default package ca

2019-01-23 16:55发布

问题:

In Java- "Static Members of the default package cannot be imported"- Can some one explain this statement? It would be better if its with an example. I am not sure if it has a really simple answer but then I tried to understand but couldn't figure it out.

回答1:

It means that if a class is defined in the default package (meaning it doesn't have any package definition), then you can't import it's static methods in another class. So the following code wouldn't work:

// Example1.java
public class Example1 {
  public static void example1() {
    System.out.println("Example1");
  }
}

// Example2.java
import static Example1.*; // THIS IMPORT FAILS
public class Example2 {
  public static void main(String... args) {
    example1();
  }
} 

The import fails because you can't import static methods from a class that's in the default package, which is the case for Example1. In fact, you can't even use a non-static import.

This bug report has some discussion about why Java acts this way, and it was eventually closed as "not a defect" -- it's the way Java was designed to behave. Default package just has some unexpected behavior, and this is one of the reasons why programmers are encouraged to never used the default package.



回答2:

as @kageb Brasee mentions : It is true that you cannot do the static import or non-static import of the class which is in a default package.

but there is a case where you can use the class (of default package) in another class: -> And this can only be done if and only if the class (in which you want to use the class of default package) is also present in a default package

if both the classes are in default packages (no matter at what location they are present) then you can use them (note : we are not import them just using them)

eg. if i want to import a class temp.class (which is in a default package) located at Home/files/temp.class into my program use.java

then just set the CLASSPATH while compiling it you can do that in two ways : permanent set OR temporary set (Not using technical terms )

permanent set : by setting the CLASSPATH (which is an environment variable) variable (different methods to do that for different OS's) -> for mac - - > export CLASSPATH=Home/files/ in this method the CLASSPATH environment variable is set till your terminal is open

so in this case :

 export CLASSPATH=Home/files/
 javac use.java
 java use

temporary set : in this method we use either of two option provided for both java and javac (java compiler) tool and they are -classpath and -cp (both these do the same job, its just -cp is short for the -classpath), in this method of setting classpath for the other files the main difference is that in this type the address(path) of the file is set only for the time period while that command (operation) is executing as soon as the statement execution complete the value of CLASSPATH(the environment) -> again reaches to the same path as it was earlier,

Note: by default the CLASSPATH is . (i.e. representing the same directory)

And in this case :

 java -cp .:Home/files use.java    // Note: don't forget . and : is for separating the different paths
 java use

Hope it helped :)