Classes & Objects

The Course
•
5 min read

A class describes a kind of object: the data it holds and the operations it can perform. An object is one instance of that class. You have already used String, arrays and ArrayList objects. Now you will define a small class representing a study session instead of using unrelated variables for every topic and duration.

The first examples put Main and Session in one file. Main has the entry-point method; Session describes the data model. The helper class is written after Main, outside its braces. Day 5 will end with those classes in separate file tabs. Do not put a second public top-level class in this single Main.java example.

Each object has its own fields

A field is a variable associated with a class or object. An ordinary instance field belongs to each object separately. new Session() creates a Session; when no constructor is declared, Java supplies a no-argument constructor. A variable such as first stores a reference to that object, not a complete embedded copy. Use first.minutes to access a field through first. That object is called the receiver: the object receiving the field access or method call.

An instance method also uses a receiver. first.describe() reads the first object’s fields, while second.describe() reads the second object’s fields. Our previous static helpers received every value as an argument. An instance method can use its object’s stored state without asking the caller to pass it repeatedly.

One class, two separate objects Java
public class Main {
    public static void main(String[] args) {
        Session first = new Session();
        first.topic = "Java";
        first.minutes = 25;

        Session second = new Session();
        second.topic = "Reading";
        second.minutes = 10;

        System.out.println(first.describe());
        System.out.println(second.describe());
    }
}

class Session {
    String topic;
    int minutes;

    String describe() {
        return topic + ": " + minutes + " min";
    }
}
Check your prediction
Java: 25 min
Reading: 10 min

Both instances use the same class definition, but hold different topic and minute values. The method is not static because it describes one particular receiver. There is no need for inheritance to represent this data.

A second reference is not a second object Java
public class Main {
    public static void main(String[] args) {
        Session first = new Session();
        first.minutes = 25;
        Session alias = first;

        alias.minutes = 40;

        System.out.println(first.minutes);
        System.out.println(first == alias);
    }
}

class Session {
    int minutes;
}
Check your prediction
40
true

Only one new expression ran, so both variables refer to the same Session. This differs from copying an int value. Draw arrows from both variables to one object; compare with the two-new example above.

Fields have defaults; local variables need initialization Java
public class Main {
    public static void main(String[] args) {
        Session session = new Session();

        System.out.println(session.minutes);
        System.out.println(session.done);
        System.out.println(session.topic);
    }
}

class Session {
    int minutes;
    boolean done;
    String topic;
}
Check your prediction
0
false
null

Instance fields start with type-specific defaults. A null topic refers to no String. These defaults are not necessarily meaningful domain values; the constructor lesson will require useful initial data instead of relying on temporary invalid state.

Your turn

Exercise

Create independent counters

Define a Counter class with an int value field and a void increment() instance method. Create two counters, increment the first twice and the second once, then print their values as 2 and 1. Do not make value static.

Your workspace Java
public class Main {
    public static void main(String[] args) {
        // Create and use two separate Counter objects.
    }
}

class Counter {
    int value;
    // Define the increment method.
}
Hint 1: the idea

Each new expression creates its own value field; calling increment changes only that call’s receiver.

Hint 2: a first step

Write the increment method without static so it works with the current object’s value. Use two new expressions in main.

Show one solution
public class Main {
    public static void main(String[] args) {
        Counter first = new Counter();
        Counter second = new Counter();

        first.increment();
        first.increment();
        second.increment();

        System.out.println(first.value);
        System.out.println(second.value);
    }
}

class Counter {
    int value;

    void increment() {
        value++;
    }
}
2
1

Change it and explain

Replace second = new Counter() with second = first and predict the result. Both references now point at one object, so all three increments affect the same value. Restore the two independent instances afterward.

For teaching this first model, fields are directly accessible. That is a starting point, not the final design: callers could store negative minutes or forget the topic. Constructors and private state will provide a clearer boundary in the next two lessons. Keep the model small enough that you can trace every mutation.

Was this article helpful?

Your response is saved on this device.