We already have discussed about what is the package? And how to import the packages into the classes out from the package. We have also learnt about the access modifiers and their scopes of visibility in the package itself and outside from the package. Let's revise few points and go deeper into to learn more about the packages.
Why packages are so important?
Packages are actually directories in which the classes are stored. So, the packages provide structure and organize all the related classes, interfaces and other components. Packages give the programmers to have flexibility to have the same names as other programmers and provide the way to reuse other programmers' and built-in Java APIs. Packages are one important part of Java's feature encapsulation. So packages are, essentially the containers of related classes and interfaces.
How the package can be used by other packages?
Let's see the same example in the earlier post about package. Here is the package named as cards and there are four classes in the package cards - spade, club, diamond, and heart.
Here in the following example, I use directly the package name and classes's names to access the class constructors.
// Class spade in package logicblocks.cards
package logicblocks.cards;
public class spade {
public spade() {
System.out.println("Spade card created");
}
}
// Class heart in package logicblocks.cards
package logicblocks.cards;
public class heart {
public heart() {
System.out.println("Heart card created");
}
}// Class club in package logicblocks.cards
package logicblocks.cards;
public class club {
public club() {
System.out.println("Club card created");
}
}// Class diamond in package logicblocks.cards
package logicblocks.cards;
public class diamond {
public diamond() {
System.out.println("Diamond card created");
}
}// Class cardsdemo in package logicblocks outside from package cards
package logicblocks;
public class cardsdemo {
public static void main(String args[]) {
// Classes in the package cards are accessed directly using pacakge path, name and class names
logicblocks.cards.spade sp = new logicblocks.cards.spade();
logicblocks.cards.heart hr = new logicblocks.cards.heart();
logicblocks.cards.club cl = new logicblocks.cards.club();
logicblocks.cards.diamond dm = new logicblocks.cards.diamond();
}
}
To avoid the typing repeatedly or redundantly, the package can be imported in the start of the program itself like in below example. It saves repeated code, avoiding writing wrong names and also makes more convenient for the programmers. The individual classes can be imported by using the individual class names after the package name.
// Class cardsdemo in package logicblocks outside from package cards
package logicblocks;
// Individual classes in the package cards are imported using pacakge path and name
import logicblocks.cards.spade;
import logicblocks.cards.heart;
import logicblocks.cards.club;
import logicblocks.cards.diamond;
public class cardsdemo {
public static void main(String args[]) {
spade sp = new spade();
heart hr = new heart();
club cl = new club();
diamond dm = new diamond();
}
}
Or all the classes can be imported in one import statement by using * after the package name.
// Class cardsdemo in package logicblocks outside from package cards
package logicblocks;
// All the classes in the package cards are imported using pacakge path and name
// * means all the classes and interfaces in the package cards can be accessed through the program
import logicblocks.cards.*;
public class cardsdemo {
public static void main(String args[]) {
spade sp = new spade();
heart hr = new heart();
club cl = new club();
diamond dm = new diamond();
}
}
Java's Standard Packages
Java's built-in API (Application Program Interface) or class library is contained in the packages. There are a large number of standard classes that are available to all the programs. The package java is at the top of the packages hierarchy. There are few packages which are used commonly, as I have mentioned in my earlier post about the package -
Package Name | Description |
---|---|
java.lang | Provides classes that are fundamental to the design of the Java programming language. |
java.io | Provides for system input and output through data streams, serialization and the file system. |
java.net | Provides the classes for implementing networking applications. |
java.applet | Provides the classes necessary to create an applet and the classes an applet uses to communicate with its applet context. |
java.awt | Contains all of the classes for creating user interfaces and for painting graphics and images. |
java.util | Contains the collections framework, some internationalization support classes, a service loader, properties, random number generation, string parsing and scanning classes, base64 encoding and decoding, a bit array, and several miscellaneous utility classes. |
java.time | The main API for dates, times, instants, and durations. |
java.text | Provides classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages. |
java.math | Provides classes for performing arbitrary-precision integer arithmetic (BigInteger) and arbitrary-precision decimal arithmetic (BigDecimal). |