Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why some Java codes imports same package again? [duplicate]

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.

like image 616
Gugan Avatar asked Dec 06 '12 07:12

Gugan


People also ask

What is the use of importing a specific class rather than entire package in Java?

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.

What is the difference between import package * and import package classname?

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.

Which package is automatically imported in every Java 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).

Can you have multiple packages Java?

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.


3 Answers

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.

like image 57
Jon Skeet Avatar answered Sep 20 '22 08:09

Jon Skeet


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

like image 34
Azodious Avatar answered Sep 19 '22 08:09

Azodious


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.

like image 29
Martin Seeler Avatar answered Sep 17 '22 08:09

Martin Seeler