Java Classes and Objects

A Java class is a blueprint that describes a kind of value your program can create. An object is a real instance made from that class, with its own field values and methods.

Classes and objects are central to object-oriented Java. A class says what data an object has and what actions it can perform.

Define A Class

Use the class keyword to define a class. Inside the class, you can add fields and methods.

class Dog {
    String name;
    int age;

    void bark() {
        System.out.println(name + " says woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.name = "Buddy";
        dog.age = 4;

        System.out.println(dog.name + " is " + dog.age + " years old.");
        dog.bark();
    }
}

Output:

Buddy is 4 years old.
Buddy says woof!

The Dog class has two fields: name and age. It also has one method: bark(). The Main class contains the main method that runs the program.

Create An Object

To create an object, use the new keyword with the class name, such as Dog dog = new Dog();. This line creates a new Dog object and stores a reference to it in the variable named dog.

The left side, Dog dog, declares a variable that can refer to a Dog object. The right side, new Dog(), creates the object.

Use The Dot Operator

Use the dot operator, ., to access an object’s fields and methods.

  • dog.name accesses the name field.
  • dog.age accesses the age field.
  • dog.bark() calls the bark method.

Fields store object data. Methods use or change that data.

Multiple Objects From One Class

A single class can create many objects. Each object has its own copy of the instance fields.

class BankAccount {
    String owner;
    double balance;

    void deposit(double amount) {
        balance += amount;
    }

    void showBalance() {
        System.out.println(owner + " has $" + balance);
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount first = new BankAccount();
        first.owner = "Ava";
        first.balance = 100.0;

        BankAccount second = new BankAccount();
        second.owner = "Noah";
        second.balance = 50.0;

        first.deposit(25.0);
        second.deposit(10.0);

        first.showBalance();
        second.showBalance();
    }
}

Output:

Ava has $125.0
Noah has $60.0

Both variables refer to BankAccount objects, but the objects are separate. Changing first.balance does not change second.balance.

Objects Are Reference Values

An object variable stores a reference to an object, not a separate copy of the whole object. If two variables refer to the same object, changes through one variable are visible through the other.

class Counter {
    int value;

    void increase() {
        value++;
    }
}

public class Main {
    public static void main(String[] args) {
        Counter a = new Counter();
        Counter b = a;

        a.increase();
        b.increase();

        System.out.println(a.value);
        System.out.println(b.value);
    }
}

Output:

2
2

a and b refer to the same Counter object. There is only one value field in that object, so both print 2.

Class Names And File Names

In beginner examples, you often place helper classes above public class Main. A Java file can contain several classes, but only one top-level class in the file can be public. For these lessons, complete programs use public class Main so they compile as a single file.

Common Mistakes

  • Forgetting new when creating an object.
  • Trying to use a field before assigning it a useful value.
  • Calling a method without parentheses, such as writing dog.bark instead of dog.bark().
  • Expecting two object variables to be separate when one was assigned from the other.

Takeaway: a class defines the fields and methods, while each object created from that class holds its own state and can run those methods.