Java Source File Organization and Package Structure

Source File Declaration Guidelines

When organizing Java source files containing multiple classes with import and package statements, follow these essential rules:

  • Each source file may contain only one public class
  • Multiple non-public classes can exist in the same source file
  • The source filename must match the public class name exactly (e.g., public class Manager requires Manager.java)
  • Package declarations must appear as the first line when classes belong to a package
  • Import statements should be placed between package declarations and class definitions
  • Without package statements, import statements appear at the file beginning
  • Package and import declarations apply uniformly to all classes within the source file

Java supports various class types including abstract and final classes with different access levels, which will be covered in acces control discussions. Additional specialized class types include inner classes and anonymous classes.

Java Package System

Packages provide organization for classes and interfaces. As Java applications grow to include hundreds of classes, systematic categorization becomes essential for maintainability.

Import Statement Usage

The import statement helps Java compilers locate classes by providing qualified paths. When complete package and class names are specified, the compiler can efficiently find corresponding source code or compiled classes.

Example import statement that loads all classes from the java.io package:

import java.io.*;

Practical Implementation Example

This example demonstrates two classes: Worker and WorkerDemo. Create a new file named Worker.java with the following content:

Worker.java Implementation

import java.io.*;

public class Worker {
    String employeeName;
    int employeeAge;
    String jobTitle;
    double compensation;
    
    // Worker class constructor
    public Worker(String employeeName) {
        this.employeeName = employeeName;
    }
    
    // Assign employee age
    public void setAge(int ageValue) {
        employeeAge = ageValue;
    }
    
    // Assign job designation
    public void setJobTitle(String title) {
        jobTitle = title;
    }
    
    // Assign salary amount
    public void setSalary(double salaryAmount) {
        compensation = salaryAmount;
    }
    
    // Display employee information
    public void displayWorkerInfo() {
        System.out.println("Employee Name: " + employeeName);
        System.out.println("Age: " + employeeAge);
        System.out.println("Position: " + jobTitle);
        System.out.println("Salary: " + compensation);
    }
}

Java program execution begins with the main method. To execute this program, create a separate class containing main method that instantiates objects.

The WorkerDemo class below creates two Workeer instances and invokes methods to assign values. Save this as WorkerDemo.java:

WorkerDemo.java Implementation

import java.io.*;

public class WorkerDemo {
    public static void main(String[] args) {
        // Create two objects using constructor
        Worker staff1 = new Worker("EMPLOYEE1");
        Worker staff2 = new Worker("EMPLOYEE2");
        
        // Invoke methods on both objects
        staff1.setAge(28);
        staff1.setJobTitle("Senior Developer");
        staff1.setSalary(1200);
        staff1.displayWorkerInfo();
        
        staff2.setAge(22);
        staff2.setJobTitle("Junior Developer");
        staff2.setSalary(600);
        staff2.displayWorkerInfo();
    }
}

Compile both files and execute WorkerDemo to see the following output:

$ javac WorkerDemo.java
$ java WorkerDemo
Employee Name: EMPLOYEE1
Age: 28
Position: Senior Developer
Salary: 1200.0
Employee Name: EMPLOYEE2
Age: 22
Position: Junior Developer
Salary: 600.0

Tags: java Source Files Package import Class Declaration

Posted on Sat, 23 May 2026 17:50:26 +0000 by ohjay