Wednesday, March 12, 2014

Importing Other Classes

Importing Other Classes
ava provides the keyword import to indicate that you want to use a
package or a class from a package. For example, to use the java.io.File class,
you must have the following import statement:
import java.io.File;

public class Demo {
    //...
}

import statements
  1. 'import' statements must come after the package statement but before the class declaration.
  2. The import keyword can appear multiple times in a class.
import java.io.File;
import java.util.List;

public class Demo {

    //...
}
import all classes
You can import all classes in the same package by using the wild character
*. For example, the following code imports all members of the java.io package.
import java.io.*;

public class Demo {
    //...
}
However, to make your code more readable, it is recommended
that you import a package member one at a time.
import java.io.File;
import java.io.FileReader;

fully qualified name
Members of the java.lang package are imported automatically. Thus,
to use the java.lang.String, for example, you do not need to explicitly import the class.
The only way to use classes that belong to other packages without
importing them is to use the fully qualified names of the classes in your code. For example,
the following code declares the java.io.File class using its fully qualified name.
java.io.File file = new java.io.File(filename);
If you class import identically-named classes from different packages,
you must use the fully qualified names when declaring the classes.
For example, the Java core libraries contain the classes java.sql.
Date and java.util.Date. In this case, you must write the fully qualified names of java.sql.
Date and java.util.Date to use them.


http://www.java2s.com/Tutorial/Java/0100
__Class-Definition/Catalog0100__Class-Definition.htm





 










































»»  READMORE...