- Java Programming Basics
- Java Tutorial
- Java Environment Setup
- Java Separators
- Java Data Types
- Java Variables
- Java Variable Scope
- Java Type Casting
- Java Operators
- Java Increment Decrement
- Java Left Shift
- Java Right Shift
- Java Bitwise Operators
- Java Ternary Operator
- Java Control Statements
- Java if-else statement
- Java for Loop
- Java while Loop
- Java do-while Loop
- Java switch Statement
- Java break Statement
- Java continue Statement
- Java Popular Topics
- Java Arrays
- Java Multidimensional Array
- Java Strings
- Java Methods
- Java Date and Time
- Java Exception Handling
- Java File Handling
- Java OOP
- Java Classes and Objects
- Java Constructors
- Java Constructor Overloading
- Java Object as Parameter
- Java Returning Objects
- Java Encapsulation
- Java Abstraction
- Java Inheritance
- Java Polymorphism
- Java Packages
- Java Import Statement
- Java Multithreading
- Java Suspend Resume Stop Thread
- Java Programming Examples
- Java Programming Examples
Java import Statement
In Java, the import statement is used to bring certain classes, or entire packages, into visibility. As soon as it is imported, a class can be referred to directly by using only its name.
The "import" statement is a convenience to the programmer and is not technically needed to write a complete Java program. If you are going to refer to a few dozen classes in your application, the import statement will save a lot of time and typing.
In a Java source file, the import statements occur immediately following the package statement (if it exists) and before any class definitions.
Java import statement syntax
Below is the general form of the import statement:
import package.name.ClassName;
"package.name" refers to the package in which the class "ClassName" is defined. The "import" statement is used to make the class available in the current source code file without the need to use its fully qualified name, that is, including the package name every time the class is used.
For example, in the following code fragment, the first Java statement imports all the classes defined in the "java.io" package, whereas the second statement imports only the "Date" class of the "java.util" package.
import java.io.*; import java.util.Date;
These are built-in packages that are already defined by the Java language creator. There are many packages that help Java programmers do most of the tasks using classes and methods defined in these types of built-in packages. You can find all the packages that are available on the "Oracle" website, the official one.
If a class with the same name exists in both of the different packages that you imported using the star form, the compiler will remain silent unless you attempt to use one of the classes. In that case, you will get a compile-time error and have to explicitly name the class after determining its package.
It must be noted that the import statement is optional. At any place where you use a class name, you can use its fully qualified name, which includes its full package hierarchy. For example, the following code fragment uses an import statement:
import java.util.*; class MyDate extends Date { }
The same example without the import statement looks like this:
class MyDate extends java.util.Date { }
Java import statement example
Here is an example program on import statements in Java. The following Java code defines the package named "DemoPackage."
package DemoPackage; public class Amount { private String name; private double bal; public Amount(String name, double bal) { this.name = name; this.bal = bal; } public void display() { System.out.println(name + ": $" + bal); } }
As you can see here, the Amount class is now public. And its constructor and its display() method are also public. It means that they can be accessed by any type of code outside the package DemoPackage. For example, here, the TestAmount imports DemoPackage and is then able to make use of the Amount class:
import DemoPackage.Amount; public class TestAmount { public static void main(String[] args) { Amount dis = new Amount("Edwin", 3240.69); dis.display(); } }
As an experiment, remove the public specifier from the Amount class and try compiling the TestAmount. Errors will result, as explained.
The output of the above program should be:
Edwin: $3240.69
This is because the "Amount" class in the "DemoPackage" package has been imported using the "import DemoPackage.Amount" statement in the "TestAmount" class. The "main" method of the "TestAmount" class creates a new "Amount" object with a name of "Edwin" and a balance of 3240.69 using the constructor "Amount(String name, double bal)." Then, it calls the "display" method of the "Amount" object, which prints out the name and balance of the object to the console.
« Previous Tutorial Next Tutorial »