Table of Contents

Java Creating Packages

Creating a package in java is quite easy, simply include a package command followed by name of the package as the first statement in the java source file.


package package_name;


package emp; // package declaration
public class employee // class declaration
   String empId;
   String name;

The above statement will create a package with the name emp in the project directory. Java uses file system directories to store packages. For example, the .java file for any class you define to be part of emp package must be stored in a directory called emp.

Additional points about packages:

  • Package names are written in all lower case to avoid conflict with the names of classes or interfaces.
  • Package statement must be first statement in the program even before the import statement.
  • All classes of the package which we wish to access outside the package must be declared public.
  • A package is always defined as a separate folder having the same name as the package name.
  • Store the listing as the file in the sub directory.
  • All classes of the package must be compiled before use.

Valid and Invalid packages names:

package employee;  // ✔️ valid
package EMPLOYEE;  // ❌ invalid

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?