Using Multiple Source Files

Introduction

Large programs are divided into multiple source files for better organization. Each class is typically stored in its own .java file.


File Structure

Rule:

  • One public class per file
  • File name = Class name
  • Example: Student class → Student.java

Example: Multiple Files

File 1: Student.java

public class Student {
    private String name;
    private int rollNo;
    private double marks;

    public Student(String name, int rollNo, double marks) {
        this.name = name;
        this.rollNo = rollNo;
        this.marks = marks;
    }

    public String getName() {
        return name;
    }

    public int getRollNo() {
        return rollNo;
    }

    public double getMarks() {
        return marks;
    }

    public void display() {
        System.out.println("Name: " + name);
        System.out.println("Roll No: " + rollNo);
        System.out.println("Marks: " + marks);
    }
}

File 2: Main.java

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("John", 101, 85.5);
        Student s2 = new Student("Alice", 102, 92.0);

        s1.display();
        System.out.println();
        s2.display();
    }
}

Compilation and Execution

Method 1: Compile All Files

javac Student.java Main.java
java Main

Method 2: Compile Main (auto-compiles dependencies)

javac Main.java
java Main

Method 3: Compile All Java Files

javac *.java
java Main

Multiple Class Example

File 1: Book.java

public class Book {
    private String title;
    private String author;
    private double price;

    public Book(String title, String author, double price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    public void display() {
        System.out.println("Title: " + title);
        System.out.println("Author: " + author);
        System.out.println("Price: " + price);
    }

    public double getPrice() {
        return price;
    }
}

File 2: Library.java

public class Library {
    private String name;
    private Book[] books;
    private int count;

    public Library(String name, int capacity) {
        this.name = name;
        this.books = new Book[capacity];
        this.count = 0;
    }

    public void addBook(Book book) {
        if (count < books.length) {
            books[count] = book;
            count++;
            System.out.println("Book added");
        } else {
            System.out.println("Library full");
        }
    }

    public void displayAll() {
        System.out.println("Library: " + name);
        System.out.println("Total books: " + count);
        for (int i = 0; i < count; i++) {
            System.out.println("\nBook " + (i + 1) + ":");
            books[i].display();
        }
    }
}

File 3: TestLibrary.java

public class TestLibrary {
    public static void main(String[] args) {
        Library lib = new Library("City Library", 10);

        Book b1 = new Book("Java Programming", "John", 499);
        Book b2 = new Book("Python Basics", "Alice", 399);
        Book b3 = new Book("Web Development", "Bob", 599);

        lib.addBook(b1);
        lib.addBook(b2);
        lib.addBook(b3);

        lib.displayAll();
    }
}

Compile and Run:

javac Book.java Library.java TestLibrary.java
java TestLibrary

Benefits of Multiple Files

  1. Organization: Each class in separate file
  2. Reusability: Classes can be used in multiple programs
  3. Maintainability: Easy to find and modify code
  4. Team Work: Different people work on different files
  5. Compilation: Only changed files need recompilation

Package Structure (Optional)

project/
├── Student.java
├── Teacher.java
├── Course.java
└── Main.java

Or with packages:

project/
├── models/
│   ├── Student.java
│   ├── Teacher.java
│   └── Course.java
└── Main.java

Rules to Remember

  1. One public class per file
  2. File name must match public class name
  3. File extension: .java
  4. Classes in same directory can access each other
  5. Compile before running

Common Mistakes

Mistake 1: File name mismatch

// File: Test.java
public class Student {  // ❌ Wrong
    // ...
}

// Should be:
// File: Student.java
public class Student {  // ✓ Correct
    // ...
}

Mistake 2: Multiple public classes

// File: Test.java
public class Student { }  // ❌ Error
public class Teacher { }  // ❌ Only one public class allowed

Simple Project Structure

File: Person.java

public class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void display() {
        System.out.println(name + ", " + age);
    }
}

File: Employee.java

public class Employee extends Person {
    int empId;
    double salary;

    public Employee(String name, int age, int empId, double salary) {
        super(name, age);
        this.empId = empId;
        this.salary = salary;
    }

    public void display() {
        super.display();
        System.out.println("ID: " + empId + ", Salary: " + salary);
    }
}

File: Test.java

public class Test {
    public static void main(String[] args) {
        Person p = new Person("John", 30);
        Employee e = new Employee("Alice", 25, 101, 50000);

        p.display();
        System.out.println();
        e.display();
    }
}

Exam Tips

Remember:

  1. One public class per file
  2. File name = Class name
  3. Extension: .java
  4. Compile: javac *.java
  5. Run: java MainClass
  6. Classes in same folder can access each other
  7. Better organization and reusability

Common Questions:

  • Why use multiple files?
  • How many public classes per file?
  • File naming rule?
  • How to compile multiple files?
  • Benefits of multiple files?