Java Tutorials
Java Methods
Java Classes
Java File Handling
The package in Java is used to integrate related classes. Think of it as a folder in a file index. We use packages to avoid word conflicts, and write better coded code. Packages are divided into two categories:
The Java API is a library of pre-written classes, free to use, integrated into the Java Development Environment.
The library contains components for input management, site editing, and much more. The full list can be found on the Oracles website: https://docs.oracle.com/javase/8/docs/api/.
The library is divided into packages and classrooms. Which means you can import one class (with its methods and features), or a complete package that contains all the classes that are part of the specified package.
To use a class or package from the library, you need to use an import
keyword:
import package.name.Class; // Import a single class
import package.name.*; // Import the whole package
When you find a class you want to use, for example, a Scanner
class, which is used to find user input, enter the following code:
import java.util.Scanner;
In the example above, java.util
is a package, while Scanner
is a package category of java.util
To use the Scanner
class, create a class object and use any of the methods available in the Scanner
class. In our example, we will use the following method nextLine()
, which is used to read the complete line:
Using the Scanner
class to get user input:
import java.util.Scanner;
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
String userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}
}
There are many packages to choose from. In the previous example, we used a Scanner
class from the java.util
package. This package also contains date and time resources, a random number generator and other help classes.
To import an entire package, complete the sentence with an asterisk (*
). The following example will include ALL classes in the java.util
package:
import java.util.*;
To create your own package, you need to understand that Java uses the file system directory to store them. As folders on your computer:
└── root
└── mypack
└── MyPackageClass.java
To create a package, use the package
keyword:
package mypack;
class MyPackageClass {
public static void main(String[] args) {
System.out.println("This is my package!");
}
}
Save the file as MyPackageClass.java, and merge it:
C:\Users\Your Name>javac -d . MyPackageClass.java
Then assemble the package:
C:\Users\Your Name>java mypack.MyPackageClass
This forces the compiler to create a "mypack" package.
The -d
keyword specifies the location where the class file is stored. You can use any reference name, such as c: / user (windows), or, if you want to save a package within the same directory, you can use the dot ".
", as in the example above.
Note: The name of the package should be capitalized to avoid conflict with class names.
When we merged the package in the example above, a new folder was created, called "mypack".
To use the MyPackageClass.java file, type the following:
C:\Users\Your Name>java mypack.MyPackageClass
The output will be
This is my package!