Java course

Classes, constructors, and objects

1.7.1 — Classes, constructors, and objects

A class describes the data and actions of a kind of object. An object is one instance of that class. A field stores data; a method performs an action or returns a value.

String stores text, int stores whole numbers, and System.out.println(...) prints a value.

Each code cell is a complete program with its own classes and inputs. Java starts in main; the statements inside its braces run in order. The other class declarations describe the objects used by main. Run each cell as a whole.

Create an object and read its fields

new Booking() creates a Booking object. The variable booking holds a reference to it. Use a dot to access its fields.

This first class has no written constructor, so Java supplies a constructor with no arguments. Its fields are filled in by the assignments in main.

Give the object starting values

A constructor runs when an object is created. It has the same name as the class and no return type. Its parameters supply starting values.

In this.name = name;, this.name means the field of the new object; name means the constructor parameter. The same pattern sets participants.

Each object has its own fields

private prevents code in another class from directly accessing a field. getParticipants() reads the value, and addParticipant() changes it. These are instance methods: you call them on a particular object.

The next cell creates two objects. Calling first.addParticipant() changes only first.

Static fields belong to the class

An ordinary field such as participants belongs to each object. A static field belongs to the class and is shared by its objects.

Here, each constructor call increases the shared createdCount. The static method is called as Booking.getCreatedCount(); it does not need a particular Booking object.

Reject invalid starting values

A participant count must not be negative. The constructor checks this rule before storing the value. throw new IllegalArgumentException(...) stops construction and reports the problem.

The try block attempts the operation. The catch block handles that exception and prints its message. A count of zero is allowed.

Check changes before storing them

A rule that must always hold is an invariant. Here it is: participants must stay at least zero. Private fields and checked methods keep outside code from bypassing that rule.

setParticipants(...) checks its argument before assignment. A rejected change leaves the previous value intact.

Practice: increase the count by one

In the next class, addParticipant currently leaves the count unchanged. Replace participants = participants; with participants = participants + 1;.

Leave main unchanged. After running the cell, it should print 3.

Hint

Change only the assignment inside addParticipant. Read the current count, add 1, then store that result in the same field.

Worked solution
public class PracticeObject {
    public static void main(String[] args) {
        Booking booking = new Booking("Ada", 2);
        booking.addParticipant();
        System.out.println(booking.getParticipants());
    }
}

class Booking {
    private String name;
    private int participants;

    Booking(String name, int participants) {
        this.name = name;
        this.participants = participants;
    }

    String getName() {
        return name;
    }

    void addParticipant() {
        participants = participants + 1;
    }

    int getParticipants() {
        return participants;
    }
}

Try other starting counts

In your completed cell, change only the second argument in new Booking("Ada", 2):

Starting countCount after one call
01
12
23

Each run creates a new object with the starting count you supply.

Enable JavaScript to run the Java notebook.