Tutorials

Java OOP Tutorial

All Tutorials Learning Tools Login

Java OOP Tutorial · Chapter 7 of 10

Multiple Files

Separate responsibilities across files.

Main.java creates objects. Student.java defines the Student type.

This is how larger Java projects stay organized.

Multi-file sample Try it Yourself »
public class Main {
    public static void main(String[] args) {
        Student one = new Student("Ana Santos", "BSIT");
        Student two = new Student("Luis Gomez", "BSCS");

        one.introduce();
        two.displayProfile();
    }
}
Student.java
public class Student {
    private String name;
    private String course;

    public Student(String name, String course) {
        this.name = name;
        this.course = course;
    }

    public String getName() {
        return name;
    }

    public String getCourse() {
        return course;
    }

    public void introduce() {
        System.out.println("Hello, I am " + name + " from " + course + ".");
    }

    public void displayProfile() {
        System.out.println("Name: " + name);
        System.out.println("Course: " + course);
    }
}

Classroom Tips

  • Open Practice Mode -> OOP Multiple Files for drills.
  • Add fields or methods as challenge work.