Packages Introduction
- A package is a namespace that organizes a set of related classes, interfaces, enums, and annotations.
- A package indicates the directory or folder in your file system where you can place your project files.
- Package can be defined as a namespace to identify classes uniquely.
- Package can be used to specify the access scope of your class or class members.
3.6.1 Package Declaration
Syntax:
package <package Name>;
Examples:
package p1;
package p2;
Example Programs
Lab450.java (Without Package)
class Lab450 {
public static void main(String str[]) {
System.out.println("Hello Guys!");
}
}
Lab451.java (With Package)
package p1;
class Lab451 {
public static void main(String str[]) {
System.out.println("Hello Guys!");
}
}
Steps to Compile WITHOUT Package
Syntax:
javac <sourceFileName>.java
Example:
javac Lab450.java
Steps to Run WITHOUT Package
Syntax:
java <class Name>
Example:
java Lab450
Steps to Compile WITH Package
Syntax:
javac -d <location> <sourceFileName>.java
Example:
javac -d . Lab451.java
Steps to Run WITH Package
Syntax:
java <package Name>.<class Name>
Example:
java p1.Lab451
Important Notes
- In the above examples:
Lab450is written without package declaration.Lab451is written with package declaration (package p1).
- When you write classes without any package declaration, those classes are placed in the default package.
- Default package has no name.
- When you compile files in default package,
.classfiles are placed in the current folder only.
🔥 Quick Understanding
-
Package = folder structure + namespace
-
Helps avoid naming conflicts
-
Controls accessibility
-
Required for large applications
-
When you are writing classes with a package declaration statement, then those classes will be placed in the specified package. → When you compile source files with a package declaration, the compiled
.classfiles will be placed in the specified package. -
When a class is specified inside a package, then that class must always be referred using the package name, which is also called the Fully Qualified Class Name (FQCN).
Example:
p1.Lab451
Directory Structure Example
3.6.2 Organizing Package Names
- Package name is a combination of multiple words separated by dot (.) symbol.
- Each word in the package name represents one folder/directory in the file system.
Examples:
package com.javaworld.sms.struts;
package com.javaworld.sms.hibernate;
package com.javaworld.sms.ejb;
Package Structure Representation
🔥 Quick Understanding
.in package → folder separation- Package = directory structure + namespace
- FQCN →
package Name.class Name - Compilation with package → creates folders automatically
Lab452.java
package com.javaworld.p1;
class Lab452 {
public static void main(String str[]) {
System.out.println("Hello Guys!");
}
}
Lab453.java
package com.javaworld.p2;
class Lab453 {
public static void main(String str[]) {
System.out.println("Hello Guys!");
}
}
Directory / Package Structure
3.6.3 Exploring Compilation
Syntax
javac -d <Location> <SourceFileName>.java
Example
javac -d . Lab.java
Important Notes
- Here
.(dot) is used as Location. → Package will be created in the current working directory and compiled classes will be placed inside that package. - If you want to create the package in a specific directory instead of current directory, then you must specify the location explicitly.
- Assume the current working directory is:
D:\JavaWorld\D1\pack
🔥 Quick Understanding
javac -d→ creates package folders automatically.means current directory- Package structure mirrors folder structure
.classfiles go inside package folders
Hello.java
package com.JavaWorldIndia;
class Hello {
public static void main(String str[]) {
System.out.println("Hello Guys!");
}
}
Case 1: Creating Package in CURRENT DIRECTORY
Relative Location
javac -d . Hello.java
Absolute Location
javac -d D:\JavaWorld\D1\pack Hello.java
Directory Structure (Case 1)
graph TD
D["D:\"] --> JavaWorld["JavaWorld"]
JavaWorld --> D1["D1"]
D1 --> PACK["PACK"]
PACK --> SRC["Hello.java"]
PACK --> com["com"]
com --> JavaWorldIndia["JavaWorldIndia"]
JavaWorldIndia --> CLASS["Hello.class"]
Case 2: Creating Package in OTHER DIRECTORY IN SAME DRIVE
Relative Location
javac -d class es Hello.java
Absolute Location
javac -d D:\JavaWorld\D1\pack\class es Hello.java
Directory Structure (Case 2)
Important Notes
javac -dcreates the package directory structure automatically.d .→ creates package folders in the current directory.d classes→ creates package folders inside theclassesdirectory.- Relative path → relative to current working directory.
- Absolute path → full folder path.
🔥 Quick Understanding
Example:
If package is:
package com.JavaWorldIndia;
Compiler creates:
com/JavaWorldIndia/
and places:
Hello.class
inside it.