Java Package and Import Mechanisms

Package System

The package system in Java serves as a fundamental organizational tool for managing code structures.

To declare a package, place a package statement on the first line of your Java source file:

package com.organization.project.module.feature;

Key package conventions include:

  • Use reverse domain name notation followed by project and module identifiers
  • All lowercase naming following standard identifier rules
  • Each package corresponds to a physical directory structure
  • Once packaged, class references become fully qualified names (package + class name)

Example package declaration:

package com.example.application.utilities;

Compilation and exceution with packages:

// Compilation
javac SourceFile.java

// Execution with fully qualified class name
java com.example.application.utilities.SourceFile

// Alternative compilation with output directory specification
javac -d output_directory source_path

Import Statements

The import mechanism enables access to classes located in different packages. Classes within the same package are automatically accessible without explicit imports.

Certain packages are automatically imported by the JVM:

  • java.lang.* - Core language features that don't require manual importing

Import statement placement occurs after package declaration but before class definitions:

package com.current.package;
import com.external.library.SpecificClass;
import com.another.library.*;

Syntax variations:

// Import single class
import com.domain.package.ClassName;

// Import all classes from package
import com.domain.package.*;

Import statements are necessary when:

  • Referencing classes outside java.lang.*
  • Accessing classes from different package hierarchies

Access Modifiers

Access control modifiers regulate visibility and accessibility of class members:

Modifier Scope Description
public Global Accessible from anywhere
protected Package + Subclasses Available within same package and derived classes
default (no modifier) Package-only Limited to classes in the same package
private Class-only Restricted to the declaring class

Visibility hierarchy:

private < default < protected < public

Top-level classes can only use public or default (package-private) access levels. Nested classes have additional flexibility with access modifiers.

Object-Oriented Programming Concepts

Core OOP principles covered include:

  • Class and object differentiation
  • Encapsulation mechanisms
  • Constructor implementation
  • this keyword usage
  • static member behavior
  • Inheritance relationships
  • final modifier applications
  • Package organization and import management

Tags: java OOP packages imports access-modifiers

Posted on Wed, 12 Aug 2026 16:46:52 +0000 by cricher