All Tools

Class Design & Object Reference Explorer

Build Java classes, create objects on the heap, and see how reference variables on the stack point to objects in memory. Understand aliasing, null references, == vs .equals(), and inheritance with method overriding.

Presets

Execute Operation

Memory Model

No objects created yet. Use the controls above to create objects.

0

Objects on Heap

0

References on Stack

0

Null References

0

Operations

Execution Log

No operations executed yet.

Reference Guide

Classes and Objects

A class is a blueprint that defines the fields (instance variables) and methods an object will have. An object is an instance of a class, created with the new keyword and stored on the heap.

Fields hold the object's state. Methods define its behavior. The constructor initializes field values when the object is created.

Every object occupies its own space on the heap. Variables in your code do not hold the object directly. Instead, they hold a reference (like an address) that points to where the object lives in memory.

Constructors and this

A constructor is a special method with the same name as the class and no return type. It runs once when you write new ClassName(args).

Inside a constructor (or any instance method), the keyword this refers to the current object. Writing this.name = name; sets the object's name field to the value passed as a parameter.

A class can have multiple constructors with different parameter lists. This is called constructor overloading. Java chooses the right one based on the number and types of arguments you pass.

Reference Semantics (== vs .equals())

When you write Dog d2 = d1;, you are not copying the object. You are copying the reference. Now both d1 and d2 point to the exact same object on the heap. Changing the object through d2 is visible through d1.

The == operator compares references. It checks whether two variables point to the same object in memory. Two different objects with identical field values will return false for ==.

The .equals() method (when properly overridden) compares the contents of two objects. Two different Dog objects with the same name and age can return true for .equals(), even though == returns false.

Inheritance and Method Overriding

Inheritance lets a subclass (extends) reuse and extend the fields and methods of a superclass. A Student extends Person automatically gets all of Person's fields and methods.

The subclass constructor must call super(args) as its first statement to initialize the inherited fields through the parent's constructor.

Method overriding happens when a subclass provides its own version of a method already defined in the superclass. When you call greet() on a Student object, Java runs the Student version, not Person's. This is the foundation of polymorphism.