Introduction to Java Packages
The following article Java Packages provides an outline for the creation of packages in java.
- The package represents an encapsulation of a set of classes, interfaces, and sub-packages. Packages make the nomenclatures well defined and in close association with the coding design context, such that the developer gets a superficial idea.
- Packages also helped control the data encapsulation, as the default and protected members of the class appear through the package scope only; they are not public to all classes.
- Before getting into the working of packages, let’s first see some terminologies – Subpackage – a subpackage is placed inside another package, like in the java.util.ArrayList, Java is the parent package, and util is the subpackage.
Working of Packages
- Directories mirror packages;, now questions are how java runtime knows where to look for the packages that the user has created?
- By default, java runtime uses the current work directory as its start point, and your user created a package in a sub-directory to the main directory, it will be found.
- Another way is to specify the directory path by setting the CLASSPATH environment variable.
- And the last way is to use the CLASSPATH option with java and javac to specify the path to the classes.
- Please note that packages should be named in order of their domain name for simplicity.
- The java compiler has to be aware of the location of a base directory always for locating the classes; for this reason, we need to set environment variables in the system.
- If we take an example of two packages awtand java.awt.event, the latter is a sub-package, hence the directory structure of later shall be containing event directory inside awt, “$BASE_DIR\java\awt\” is the address for parent package and “$BASE_DIR\java\awt\event\” is the address for sub-package.
Types of Packages
- Java offers flexibility to either use built-in java packages or uses the user-created packages based on the use case.
- The built-in packages are always important while coding, as they offer a lot; the rt.jar file carries multiple functionality definitions, which appear in the java.util.* like packages.
Now let us see built-in and user-defined packages in detail –
1. Built-in Packages
Built-in packages contain a large number of java classes, and it contains the following packages –
- lang – The object class is found in this particular package; this package is automatically imported, this package bundles up the basic classes.
- Utilise – this is a crucial package and contains many classes related to collections like ArrayList, HashMap, etc. All the data structure implementations are in this class, and you need to use them by incorporating them abstractly.
- The input-output stream handling and processing related classes are placed in this package; an example of such classes is InputStreamRe, Filereaderader, etc.
- net – this contains the classes used for performing certain networking-related operations; the example classes are Socket and SocketAddress.
- beans – contains classes related to bean development, components based on java beans architecture.
2. User-Defined Packages
- A user always has the privilege to enclose his created classes into some package; the user can define the name and directory structure of that package in his custom way only.
- Hence, the package is like a namespace carrying generally related classes, and if the package is not tagged to any class, it is put into the default package.
Example
package com.supplychains
class SupplyChainManagement
{
public void getPrompt()
{
System.out.println(“Welcome to SCM”);
}
}
This class can now be accessed in other classes by merely importing the package named as “com.supplychains”, and then class supply chain management and its member functions and member variables can be accessed.
How to Create Packages in Java?
First of all, you should have a class; let us consider the class structure we portrayed above only.
package com.supplychains
class SupplyChainManagement
{
public void getPrompt()
{
System.out.println(“Welcome to SCM”);
}
}
This class shall be saved like say “SupplyChainManagement.java” is the name we saved it with.
- Now compile this file with a javac compiler, which can be done by writing javac SupplyChainManagement.java; this will create a .class file in the same directory.
- Now we can use the command, “javac -d. SupplyChainManagement.java”, this command will result in package formation, now directory structure is a thing we have to be keen about, the “.” placed after -d in the above command represents the current working directory. So in the selected directory, a folder will be created, and a package will be formed in which the class file created in step 2 will be placed.
- The next step is to compile the package; this can be done with the following command –
“javac -d .. SupplyChainManagement.java. “
.. represents the parent directory (like C drive or D drive).
- Hence this way, multiple classes can be bundled up in a directory structure that can be accessed in the corresponding order only.
- Now you just need to use an import statement to incorporate this package in any of the java classes; note that java runtime will refer to it with respect to the path set in the environment variable, which contains the root directory only.
Conclusion
Hence, we read a little about packages in java, their creation, their working, and how can we create and import our packages from anywhere to any other classes. Packages can be encapsulating the interfaces and classes. A wide variety of built-in packages are already available to exploit the data structure and algorithms; java provides a wide variety, and multithreading is also supported via multiple concurrency packages.
Recommended Articles
This is a guide to Java Packages. Here we discuss the introduction, working, and types of the package, which include built-in and user-defined packages as well as the creation of packages in java. You may also look at the following articles to learn more –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses