Skip to content
  • Classes & Objects · Java

Classes & Objects

LearnViaNotes @learnvianotes· 3 min read

A class is a blueprint that bundles state (fields) and behaviour (methods); an object is one instance of it living on the heap.

The four pillars of OOP

Declaring a class and creating objects

Java
public class Main {
    public static void main(String[] args) {
        BankAccount a = new BankAccount("Asha", 500);
        BankAccount b = new BankAccount("Ravi", 100);
        a.deposit(250);
        b.withdraw(30);
        System.out.println(a.describe());
        System.out.println(b.describe());
        System.out.println("accounts opened: " + BankAccount.count);
    }
}

class BankAccount {
    String owner;          // instance field: one copy per object
    double balance;
    static int count = 0;  // static field: one copy per class

    BankAccount(String owner, double balance) {   // constructor
        this.owner = owner;
        this.balance = balance;
        count++;
    }

    void deposit(double amount) { balance += amount; }
    void withdraw(double amount) { balance -= amount; }
    String describe() { return owner + " has " + balance; }
}
Output
Asha has 750.0
Ravi has 70.0
accounts opened: 2

What new does

  1. Load the class

    If BankAccount isn't loaded yet, the class loader loads, links and initialises it (static fields/blocks run once).

  2. Allocate

    Memory for the object is reserved on the heap; every field gets its default value (0, false, null).

  3. Initialise

    Field initialisers and instance initializer blocks run, then the constructor body.

  4. Return a reference

    new evaluates to a reference (think: a safe pointer) that is stored in the variable.

References, not objects, live in variables

Java
public class Main {
    public static void main(String[] args) {
        Point p1 = new Point();
        p1.x = 1;
        Point p2 = p1;          // copies the reference, not the object
        p2.x = 99;
        System.out.println(p1.x);        // same object
        System.out.println(p1 == p2);    // same reference?

        Point p3 = new Point();
        p3.x = 99;
        System.out.println(p1 == p3);    // different objects

        Point nothing = null;
        System.out.println(nothing == null);
    }
}

class Point { int x, y; }
Output
99
true
false
true

Default values of fields

Field typeDefault
byte, short, int, long0
float, double0.0
char'\u0000'
booleanfalse
Any reference (String, arrays, objects)null

Instance vs class members

Instance memberStatic (class) member
Belongs toeach objectthe class itself
Copiesone per objectexactly one
Accessed viaobj.fieldClassName.field
Can use this?YesNo

What is the difference between a class and an object?

Is p2 = p1 a copy of the object?

Where do objects and references live?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report