Skip to the content.

Classes and Objects

Student Teaching Lesson

Popcorn Hack

class Book {
    String title;
    int pages;

    void printInfo() {
        System.out.println("Title: " + title + ", Pages: " + pages);
    }
}

class MainPopcorn {
    public static void main(String[] args) {
        Book myBook = new Book();
        myBook.title = "The Great Gatsby";
        myBook.pages = 180;
        myBook.printInfo();
    }
}

MainPopcorn.main(null);

Title: The Great Gatsby, Pages: 180

Homework Hack 1

class Student {
    String name;
    int grade;
    int pets;
    int siblings;
    String hobby; // your own instance variable

    void printInfo() {
        System.out.println(name + " is in grade " + grade + ", has " + pets + " pet(s), " 
                           + siblings + " sibling(s), and enjoys " + hobby + ".");
    }
}

class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "Nora";
        s1.grade = 11;
        s1.pets = 0;
        s1.siblings = 2;
        s1.hobby = "painting";

        Student s2 = new Student();
        s2.name = "Avika";
        s2.grade = 9;
        s2.pets = 1;
        s2.siblings = 1;
        s2.hobby = "soccer";

        Student s3 = new Student();
        s3.name = "Soni";
        s3.grade = 10;
        s3.pets = 3;
        s3.siblings = 0;
        s3.hobby = "playing guitar";

        s1.printInfo();
        s2.printInfo();
        s3.printInfo();
    }
}

Main.main(null);

Nora is in grade 11, has 0 pet(s), 2 sibling(s), and enjoys painting.
Avika is in grade 9, has 1 pet(s), 1 sibling(s), and enjoys soccer.
Soni is in grade 10, has 3 pet(s), 0 sibling(s), and enjoys playing guitar.

Popcorn Hack 2

class Student {
    String name;
    int grade;
    int pets;
    int siblings;
    String hobby;

    void printInfo() {
        System.out.println(name + " is in grade " + grade + ", has " + pets + " pet(s), " 
                           + siblings + " sibling(s), and enjoys " + hobby + ".");
    }
}

class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "Ava";
        s1.grade = 10;
        s1.pets = 1;
        s1.siblings = 2;
        s1.hobby = "painting";

        Student s2 = new Student();
        s2.name = "Leo";
        s2.grade = 11;
        s2.pets = 0;
        s2.siblings = 1;
        s2.hobby = "soccer";

        Student s3 = new Student();
        s3.name = "Mia";
        s3.grade = 9;
        s3.pets = 3;
        s3.siblings = 0;
        s3.hobby = "playing guitar";

        // New reference variable (nickname) pointing to the same student
        Student nickname = s2;
        nickname.name = "Leo 'Lightning'";  // update name through the new reference

        // Output attributes using both references
        System.out.println("Original reference:");
        s2.printInfo();
        System.out.println("Nickname reference:");
        nickname.printInfo();
    }
}

Main.main(null);

Original reference:
Leo 'Lightning' is in grade 11, has 0 pet(s), 1 sibling(s), and enjoys soccer.
Nickname reference:
Leo 'Lightning' is in grade 11, has 0 pet(s), 1 sibling(s), and enjoys soccer.