Packages in JavaLast Updated : 5 Jan 2026 A Java package is a group of similar types of classes, interfaces and sub-packages. This chapter explains how Java packages work and how they help in organizing, managing, and using classes in Java programs. What is Package in Java?A Java package groups related classes, interfaces, and sub-packages into one unit. It helps organize large programs, avoid name conflicts, control access, and make code reusable and modular. There are two types of Java Packages:
Java Built-in PackagesBuilt-in packages are predefined packages provided by Java that contain ready-to-use classes and interfaces. Some of the built-in packages that are commonly used are:
Java User-defined PackagesUser-defined packages are packages created by programmers to group their own classes and interfaces. They help to avoid naming conflicts, support access control, and allow developers to structure large projects in a clean and modular way. ExampleHere, we will create two Java files in two different packages (or folders). We will also create three files. One file each for both the folders, and the last file is of the main class. Observe the following. The two folders, mypackage1 and mypackage2, have been created. ![]() Inside mypackage1, create a file Class1.java. ![]() The following code is written for the Class1.java file. Similarly, create Class2.java file inside mypackage2 ![]() The following code is written for the Class2.java file. Now, outside of both packages, create the Main.java file. ![]() The following code is written for the Main.java file. ExampleOutput: Inside the package 1. Inside the package 2. Note: If we change the access specifier of the classes Class1 and Class2 from public to protected, default, or private, then the compiler will give an error. Observe the following.Save this file as Class1.java Save this file as Class2.java Now, when we run the Main.java file, the following error is observed.
.\mypackage1\Class1.java:3: error: modifier protected not allowed here
protected class Class1 { // changed the access specifier to protected
^
Main.java:2: error: Class1 is not public in mypackage1; cannot be accessed from outside package
import mypackage1.Class1;
^
Main.java:4: error: Class2 is not public in mypackage2; cannot be accessed from outside package
import myPackage2.Class2;
^
Main.java:11: error: Class1 is not public in mypackage1; cannot be accessed from outside package
Class1 obj1 = new Class1();
^
Main.java:11: error: Class1 is not public in mypackage1; cannot be accessed from outside package
Class1 obj1 = new Class1();
^
Main.java:13: error: Class2 is not public in mypackage2; cannot be accessed from outside package
Class2 obj2 = new Class2();
^
Main.java:13: error: Class2 is not public in mypackage2; cannot be accessed from outside package
Class2 obj2 = new Class2();
Explanation The error is coming because protected and default access specifiers restrict the visibility of the classes outside the package. Therefore, the main class cannot access Class1 and Class2. Note: In this example, we have created the folders mypackage1 and mypackage2 explicitly. If we want to build it using the command prompt, then do the following.First, put all three files (Class1.java, Class2.java, Main.java) in one location (in my case location is the test folder). ![]() Ensure that the access specifier of Class1 and Class2 is public in this case. Now, compile it using the following command. Here, -d is for the destination. After -d, we see a dot(.), which means in the current directory, and *.java means all the Java files. After doing the compilation work, we see the following. ![]() We see that both the packages have been created. Now, run the Main.class file using the command And we get the output as: Inside the package 1. Inside the package 2. Static Import of the PackagesThe static import feature was first introduced in Java version 5 and is still present in later versions of Java. Static import allows only public static members (methods or fields) of a class to be used in the code without specifying the name of the class in which it is defined. The following example demonstrate the static import of the packages in Java. ExampleOutput: In the main method Explanation: "System" is a class present in the java.lang package, and out is a static variable present in the System class. By using static import, we are able to access the out variable without using the class name "System". Dealing With Name Conflicts Using PackagesAs discussed earlier, name conflicts can be handled using packages. Let's see an example to understand it. ExampleOutput:
Main.java:7: error: reference to Date is ambiguous
Date d = new Date(44l);
^
both class java.util.Date in java.util and class java.sql.Date in java.sql match
Main.java:7: error: reference to Date is ambiguous
Date d = new Date(44l);
^
both class java.util.Date in java.util and class java.sql.Date in java.sql match
2 errors
Explanation In the program, java.sql and java.util packages have been imported. Each package contains the Date class. When the Date class is instantiated, the compiler does not know which Date class it should refer to, whether the compiler should refer to the Date class of the java.sql package or the Date class of java.util package. It leads to ambiguity. Therefore, the error has come. To solve this error, we have to use the class name along with the package name. The fully qualified name clearly tells the compiler which Date class it should refer to. ExampleOutput: Inside the main method. Java SubpackageA package inside a package is called a subpackage. It should be created to categorize the package further. Let's take an example, Sun Microsystems has defined a package named java that contains many classes like System, String, Reader, Writer, Socket, etc. These classes represent a particular group. For example, Reader and Writer classes are for Input/Output operations, Socket and ServerSocket classes are for networking, etc. and so on. So, Sun has subcategorized the Java package into subpackages such as lang, net, io, etc. and put the Input/Output related classes in the io package, Server and ServerSocket classes in the net package and so on. The standard for defining a package is domain.company.package for examlle com.tpointtech.bean or org.sssit.dao. The following example demonstrate the working of Subpackages in Java. ExampleTo Compile: javac -d . Main.java To Run: java com.tpointtech.core.Main Output: Hello subpackage Advantages of Using PackagesSeveral advantages of packages in Java are as follows: 1. Avoid Naming ConflictsPackages are used to avoid naming conflicts among class names. Suppose there is a class called Employee. We know that employees are in the HR as well as the IT department of a company. So, the question is, if we write Employee, then it is of which department? Here, packages come into the picture, and we can write Employee as the fully qualified name for both the department and company.hr.Employee and company.it.Employee. Thus, the naming conflict is avoided. 2. Organize ClassesFor organizing the class, interfaces and other components, packages are used. Think of a library. There are different shelves for the various kinds of books. Maths books are stored in one place. Science books are stored in another place, etc. Similarly, we can also group similar types of classes in one place. Packages help in doing so. In Java, built-in classes are also organized using packages. Observe the following diagram. ![]()
Package Structure and Naming RulesHere, we are going to discuss the package structure and naming rules in Java. 1. Naming ConventionsJava packages should be in lowercase, and the dot should separate the components (.). If the component is a class, then the first letter of the class name should be capitalized. To read more Java Naming Conventions 2. Structure of DirectoryThe names of the directories and the package names are closely related. For example, if the package name is company.hr.Employee, then the company is the directory, and inside it, there is another directory called hr, and inside it, there is a class called Employee. Notice that E is in capital, indicating that it is a class name. Example: Here, lang is a subpackage inside the package java. Accessing Classes from PackageHere, we are going to discuss how we can access class from packages in Java. Importing a Specific ClassWe can access classes of a package using the import keyword. For example, if we want to access a specific class of the java.util package, then use the following statement: Here, we are accessing a specific class called ArrayList of the package java.util. Similarly, if we want to include another class of the different package, we can write Here, we have imported BigInteger class of the java.math package. Importing All ClassesIf we want to use a class without using the import statement, then we have to write the following. ExampleCompile and RunOutput: Inside the main method Note: By applying the import package.*; statement, we know that it imports all the classes and interfaces present in that package. However, all the classes, interfaces, etc., will never be imported from their sub-packages.Whenever we have two packages having classes of the same name (for example, java.sql.Date and java.util.Date), it is suggested to use the fully qualified names to avoid name conflicts. We will see this later in this tutorial. Next TopicAccess-modifiers-in-java |
We request you to subscribe our newsletter for upcoming updates.