Aral Code

Object-Oriented Programming Lesson 16 of 21

Java Classes and Objects

Object-oriented programming in Java — define a class with fields and methods, create objects with new, use this, write toString, and keep classes in separate files.

Java is an object-oriented language. Instead of keeping data in loose variables and parallel arrays, you design classes that describe things in your program, such as a Student, a Product or a BankAccount, and create objects from them.

Class vs object

  • A class is a blueprint, like the design of a jeepney.
  • An object is a real thing built from it, like the actual jeepneys on the road. Each has its own plate number, color and passengers, but all follow the same design.

Defining a class

class Student {
    // fields (attributes): what a student HAS
    String name;
    int gradeLevel;
    double average;

    // methods (behaviors): what a student DOES
    void introduce() {
        System.out.println("Hi, I'm " + name + " from Grade " + gradeLevel + ".");
    }

    boolean isHonor() {
        return average >= 90;
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "Maria";
        s1.gradeLevel = 11;
        s1.average = 94.2;

        Student s2 = new Student();
        s2.name = "Jose";
        s2.gradeLevel = 12;
        s2.average = 88.5;

        s1.introduce();
        s2.introduce();
        System.out.println(s1.name + " honor? " + s1.isHonor());
        System.out.println(s2.name + " honor? " + s2.isHonor());
    }
}

Output

Hi, I'm Maria from Grade 11.
Hi, I'm Jose from Grade 12.
Maria honor? true
Jose honor? false
  • Fields are variables that belong to each object.
  • Methods without static belong to each object and can use its fields.
  • new Student() creates a new object. s1 and s2 are separate objects with their own data.
  • The dot . accesses an object's fields and methods: s1.name, s1.introduce().

Default field values

Fields you don't set get defaults: 0 for numbers, false for booleans, null for objects like String. (Local variables inside methods have no defaults and must be initialized.)

The this keyword

Inside a method, this means "the current object". It's needed when a parameter has the same name as a field:

class Product {
    String name;
    double price;
    int stock;

    void setPrice(double price) {
        this.price = price;   // field = parameter
    }

    void sell(int qty) {
        if (qty > stock) {
            System.out.println("Not enough " + name + "!");
        } else {
            stock -= qty;
            System.out.printf("Sold %d %s for ₱%.2f%n", qty, name, qty * price);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Product kape = new Product();
        kape.name = "Kape 3-in-1";
        kape.stock = 50;
        kape.setPrice(8);
        kape.sell(4);
        kape.sell(100);
        System.out.println("Stock left: " + kape.stock);
    }
}

Output

Sold 4 Kape 3-in-1 for ₱32.00
Not enough Kape 3-in-1!
Stock left: 46

toString: printing an object nicely

class Book {
    String title;
    String author;

    @Override
    public String toString() {
        return title + " by " + author;
    }
}

public class Main {
    public static void main(String[] args) {
        Book b = new Book();
        b.title = "Noli Me Tángere";
        b.author = "Jose Rizal";
        System.out.println(b);
    }
}

Output

Noli Me Tángere by Jose Rizal

Without toString, println(b) shows something like Book@5ca881b5. @Override tells Java you're replacing a method every class inherits (more in the Inheritance lesson).

Arrays of objects

class Item {
    String name;
    double price;
    Item(String n, double p) { name = n; price = p; }
}

public class Main {
    public static void main(String[] args) {
        Item[] menu = {
            new Item("Pandesal", 25),
            new Item("Kape", 8),
            new Item("Itlog", 9)
        };
        for (Item it : menu) {
            System.out.printf("%-10s ₱%.2f%n", it.name, it.price);
        }
    }
}

Output

Pandesal   ₱25.00
Kape       ₱8.00
Itlog      ₱9.00

This replaces the parallel arrays from the Arrays lesson: each item keeps its own name and price together. The Item(String n, double p) part is a constructor, explained in the next lesson.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What keyword creates a new object?
2. What does this refer to inside a method?
3. What does System.out.println(obj) show if the class has no toString?
4. What's inside new Student[5] right after it's created?

By Sir Reyel · Last updated 2026-12-04