constructor and method used in program didnt understandble

Okay, this appears to be an introduction to classes for come sort of course. What this program does is enter through the main method:

public class PassingThis {
    public static void main(String[] args) {
        new Person().eat(new Apple());
    }
}

This creates a new instance of the person class, calling this constructor:

class Person {
    public void eat(Apple apple) {
        Apple peeled = apple.getPeeled();
        System.out.println("Yummy");
    }
}

This constructor requires an instance of the Apple class, so it is called by creating a new instance of it (new Apple())

Inside of that constructor, it makes a new instance of the Apple class by calling the getPeeled() method.

Apple getPeeled() {
    return Peeler.peel(this);
}

This creates a new instance of the Peeler class which returns the apple. The Peeler class returns the now “Peeled” apple to the person class, which then prints “Yummy”

Leave a Comment