Reputation: 135
For example while importing classes from IO package in java we use import java.io.*
, instead of this why cant we use import java.io
as import statement? When i use import java.io
i get an error stating location: package java
. Why is this error thrown?
Upvotes: 2
Views: 14296
Reputation: 2208
You can only import classes not package. import java.io.*
will import all classes in java.io
package
To import all the types contained in a particular package, use the import statement with the asterisk (*
) wildcard character.
Now you can refer to any class or interface in the package by its simple name.
Note: Another, less common form of import allows you to import the public nested classes of an enclosing class. For example, if the graphics.Rectangle class contained useful nested classes, such as Rectangle.DoubleWide and Rectangle.Square, you could import Rectangle and its nested classes by using the following two statements.
import graphics.Rectangle;
import graphics.Rectangle.*;
Be aware that the second import statement will not import Rectangle.
see this page for more information on it
Upvotes: 13
Reputation: 287
import java.io;
means import class io
from package java
. but the io
class does not exist. java.io is a package, not a class.
Upvotes: 1
Reputation: 8868
In Java, you can import classes and packages. To import a class, you can use the fully qualified name of the class like
import com.pkg.spkg.ClassName;
.
The package spkg
may contain a lot of classes, and you require all of them in your application. In such a case the good idea would be to import all the classes or package itself. So to import the whole package we can use the wildcard *
like
import com.pkg.spkg.*;
Hope this makes it clear.
Upvotes: 2
Reputation: 8068
It's just a matter of the syntax. If you look how to import a specific class:
import java.util.List;
it seams consistent to express import everything from some package by using the asterisk *
like in so many other environments: pattern matching
, Ant
, etc.
import java.util.*;
The asterisk has a history to match "everything".
And due to conventions and not any Java restrictions, you would not be able to distinguish between the sub-package io
and a class named io
. There is no compile error if you name a class not starting with an uppercase letter.
Upvotes: 2
Reputation: 1
The second statement treated io
as class, not package. That's why the error. You should read it carefully and use Java Naming conventions to properly name your classes.
The correct usage of import statement if you want to use java.io
package classes
import java.io.*;
Upvotes: 1
Reputation: 314
Using * on
import java.io.*
Will import every classes in the io directory, you won't be able to import a directory.
Upvotes: 1
Reputation: 4176
import java.io.*
will import all the classes from the io package. io
is the name of the package and you need to import only classes.
You can alternatively import just the classes you need. Eg: import java.io.BufferedInputStream
Have a look at java.io docs to find all the classes defined in the java.io
package.
Also have a look at the tutorial for packages, to understand all about packages in java.
Upvotes: 1