Java Classes and Objects Explained

Classes and objects are the heart of Java. A class is a blueprint, and an object is a real thing built from it. Once this clicks, the rest of Java makes far more sense. In this guide, we'll build a small bank account class step by step and see exactly how Java classes and objects work.

What is a class in Java?

A class describes what something has and what it can do. Think of an architect's drawing of a house. The drawing shows rooms, doors, and windows, but you can't live in it. It's a plan.

In Java, a class has two main parts:

  • Fields — the data each object holds (also called instance variables or state).
  • Methods — the actions an object can perform (its behaviour).
public class BankAccount {
    // Fields: what every account has
    String owner;
    double balance;

    // Method: what every account can do
    void deposit(double amount) {
        balance = balance + amount;
    }
}

This code creates no accounts yet. It only tells Java what a bank account looks like.

What is an object in Java?

An object is an actual instance of a class that exists while your program runs. If the class is the house drawing, objects are the real houses built from it. They share a layout, but each house has its own furniture.

Every object has three things:

PropertyMeaningBank account example
StateThe current values of its fieldsowner = "Asha", balance = 250.0
BehaviourWhat its methods dodeposit() increases the balance
IdentityIt's a distinct object, even if another has identical valuesTwo accounts with $0 are still two different accounts

How do you create an object in Java?

You create objects with the new keyword:

BankAccount account = new BankAccount();
account.owner = "Asha";
account.deposit(250.0);

System.out.println(account.owner + " has $" + account.balance);
Asha has $250.0
  • new BankAccount() — builds a fresh object in memory.
  • BankAccount account — declares a variable that can refer to a BankAccount object.
  • account.owner and account.deposit(...) — the dot (.) means "reach into this object" to read a field or call a method.

Let's see it in action: one class, many objects

This is the idea beginners most often miss: each object keeps its own data. Create a few dogs below and make each one bark.

Try it: create objects from one class

Class (the blueprint)

class Dog {
    String name;
    int age;

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

Heap memory (the objects)

    No objects yet. The class alone doesn't create any — press new Dog(...).

    Console—

    The class is written once. Every new creates a separate object with its own copy of name and age. Calling bark() on one object only uses that object's data.

    What is a constructor?

    Setting fields one by one after creating an object is clumsy, and it's easy to forget one. A constructor lets you set everything up the moment the object is created.

    public class BankAccount {
        String owner;
        double balance;
    
        // Constructor: same name as the class, no return type
        BankAccount(String owner, double openingBalance) {
            this.owner = owner;
            this.balance = openingBalance;
        }
    
        void deposit(double amount) {
            balance += amount;
        }
    }
    
    // Usage
    BankAccount asha = new BankAccount("Asha", 100.0);
    BankAccount ben  = new BankAccount("Ben", 40.0);
    • BankAccount(String owner, double openingBalance) — runs automatically whenever new BankAccount(...) is called.
    • this.owner = owner — this.owner is the field, and plain owner is the parameter. The this keyword tells them apart.

    Can a class have more than one constructor?

    Yes. That's called constructor overloading. For example, you could add BankAccount(String owner) that starts the balance at zero. Java picks the right constructor based on the arguments you pass.

    What does a variable really hold? References explained

    Object variables don't hold the object itself. They hold a reference, which is like a house address written on a sticky note. This matters as soon as you copy a variable:

    BankAccount a = new BankAccount("Asha", 100.0);
    BankAccount b = a;        // copies the address, NOT the account
    
    b.deposit(50.0);
    System.out.println(a.balance);   // a and b point to the same object
    150.0

    What's null? A reference variable that points to nothing holds null. Calling a method on it, such as account.deposit(5) when account is null, throws a NullPointerException, the most common error in Java.

    How do classes fit into object-oriented programming?

    Classes and objects are the foundation for four ideas you'll meet throughout Java:

    IdeaIn plain EnglishLearn more
    EncapsulationHide an object's data and only allow changes through its methods.Access modifiers
    InheritanceBuild a new class on top of an existing one.Inheritance
    AbstractionShow what something does, hide how it does it.Abstract classes
    PolymorphismTreat different objects the same way through a shared type.Interfaces

    Quick recap

    • A class is a blueprint with fields (data) and methods (behaviour).
    • An object is an instance of a class, created with new, with its own state.
    • A constructor sets up a new object; it has the class's name and no return type.
    • Object variables hold references; copying a variable shares the same object.

    What's next?

    Right now anyone can write account.balance = -1000. Learn how to protect your fields in Java access modifiers, or go back to the Java overview.

    Frequently Asked Questions

    What is the difference between a class and an object in Java?

    A class is a blueprint that describes what data and behaviour something has. An object is a real thing built from that blueprint while the program runs. One class, such as BankAccount, can produce thousands of objects, each with its own balance.

    Can a Java class exist without objects?

    Yes. Writing a class doesn't create any objects. Objects only exist once your code runs new ClassName(). Some classes, such as utility classes full of static methods, are never turned into objects at all.

    What is a constructor in Java?

    A constructor is a special block of code that runs when an object is created with new. It has the same name as the class and no return type. Its job is to give the new object's fields sensible starting values.

    What happens if I don't write a constructor?

    Java adds an invisible no-argument default constructor for you. Fields then start at default values: 0 for numbers, false for booleans, and null for objects. As soon as you write any constructor yourself, Java stops adding the default one.

    Where are Java objects stored in memory?

    Objects live in an area of memory called the heap. Your variables don't hold the object itself; they hold a reference, which is like an address pointing to the object on the heap. The garbage collector frees objects nothing refers to anymore.