Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Object-Oriented Concepts

Java classes and objects

How Java classes and objects work: fields, methods, instances, references, equality, null and garbage collection, explained with runnable examples.

A class is a blueprint: it declares what data an object holds (fields) and what it can do (methods). An object is one concrete instance of that blueprint, created with new. Every object has its own copy of the instance fields; the methods are shared and operate on the object they are called on, which they can refer to as this.

Almost everything you work with in Java apart from the eight primitive types is an object, and variables of class type never contain the object itself. They contain a reference to it. Understanding that single fact explains most surprises about assignment, equality, null and method arguments.

Java
class Counter {
    int value;                       // field, one per object
    void increment() { value++; }    // method
}

Counter c = new Counter();           // c refers to a new object

Try it

Java Instances have their own state

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java References, equality and arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Anatomy of a class

Part Purpose
Fields Hold the state of each object. Instance fields get a default value (0, false, null) if not assigned.
Methods Define behavior; they can read and change fields and call other methods.
Constructors Initialize a new object. See constructors.
static members Belong to the class itself, not to any object (counters, factory methods, constants).
Nested types Classes, interfaces or enums declared inside a class.
Initializer blocks Run during construction. See initialization order.

A source file can hold several top-level classes, but only one may be public, and its name must match the file name when you compile with javac. The source launcher used by this site's runner does not have that restriction.

Usage notes

  • Variables hold references. Assigning one object variable to another copies the reference, not the object. Both now see the same state.
  • == compares references, equals compares contents. equals only does the latter if the class overrides it, and then it must override hashCode consistently so the object works in HashMap and HashSet.
  • Arguments are passed by value. A method receives a copy of the reference. It can modify the object it points to but cannot make the caller's variable point somewhere else.
  • null means no object. Calling a method or reading a field on null throws NullPointerException. Use Objects.requireNonNull, Optional or clear contracts to keep nulls out of your design.
  • Garbage collection. There is no delete. An object becomes eligible for collection when no reachable reference remains; you do not control exactly when it is reclaimed.
  • Default toString. Without an override it prints the class name and a hash, which is rarely useful. Override it for readable debugging.
  • Small immutable data holders are shorter as records.
  • Visibility. Fields should usually be private; expose behavior through methods (encapsulation).