Possible Duplicate:
Java import confusion
When i read play frameworks documentation, I found this.
import play.*;
import play.mvc.*;
In the first line itself they have imported all the classes under play package. Then what is the use of second line. Check this link. Go to 'Providing an application error page' section.
Correct me if i'm wrong in imports concept.
The import statement is optional, and we can use the fully-qualified name of the class to refer to a class or package in the program. This method tells the compiler that the class is defined under a particular package, and we want to use that class or classes in our program.
As discussed above the package keyword is used to group certain classes and interface under one package and, the import keyword is used include/use the classes and interface from a package in the current program.
For convenience, the Java compiler automatically imports two entire packages for each source file: (1) the java. lang package and (2) the current package (the package for the current file).
There can be only one package statement in each source file, and it applies to all types in the file. Note: If you put multiple types in a single source file, only one can be public , and it must have the same name as the source file. For example, you can define public class Circle in the file Circle.
Correct me if i'm wrong in imports concept.
You are.
Something like this:
import foo.bar.*;
only imports types from the foo.bar
package. It doesn't import from "subpackages" such as foo.bar.baz
.
It's very easy to test this:
import java.util.*;
class Test {
public void foo() {
Pattern p = null;
}
}
That won't compile, but if you add:
import java.util.regex.*;
then it will.
Java doesn't really think of packages as forming a hierarchy. We do, as humans, but as far as the Java language is concerned, the packages java.util
and java.util.regex
are entirely unrelated.
import play.*;
will import all the types within; except those which are inside a sub-package.
import play.mvc.*;
will import all the types within mvc
package, which is sub-pckage within play
.
You'll find this answer also helpful: Java import confusion
The first line will import all classes, enums and interfaces in the same level of play.* but no subpackages. Therefore you need the second line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With