Java Classes and Objects Explained with Practical Examples

Understand Java classes and objects through real, runnable examples. Learn fields, constructors, methods, encapsulation, the new keyword, and how the JVM lays out objects in memory.

Javabeginner
13 min read

Every meaningful Java program is a conversation between objects. The order you place in a coffee app, the database connection your phone opens, even the button you tap on screen — they are all instances of classes that someone defined. If classes and objects still feel abstract, the rest of Java (collections, frameworks, Android, Spring) will feel like memorising magic words. Spend twenty minutes here and the magic disappears.

This guide explains exactly what a Java class is, how an object is created from it, what happens in memory when you call new, and how to model real things using fields, constructors, and methods. Every example runs on Java 25 LTS, the current stable release in 2026.

What a Class Really Is

A class is a blueprint. It says "here is the shape of a thing, here is the data it holds, and here is what it can do." A class on its own does nothing; no memory is allocated, no behaviour runs.

An object is the concrete thing built from that blueprint. When you write new Order(...), the JVM allocates memory on the heap, fills in the fields, and hands you a reference to that block of memory. You can build a million Order objects from one Order class, and each one carries its own state.

A useful analogy: the class is the architectural plan for a house. The object is an actual house built from that plan. The plan is reusable; every house built from it has its own kitchen, its own residents, and its own address.

Fields, Constructors, and Methods

A class has three core ingredients:

  • Fields are the data. They hold the state of an object: a customer name, an order total, a connection's URL.
  • A constructor is the special method the JVM runs when you call new. It initialises the fields.
  • Methods are the behaviour. They read and modify the fields, and they let other objects ask the object to do something.

Here is a minimal class that models a coffee order:

javajava
public class Order {
  private final String id;
  private final String item;
  private int quantity;
  private double unitPrice;
 
  public Order(String id, String item, int quantity, double unitPrice) {
    this.id = id;
    this.item = item;
    this.quantity = quantity;
    this.unitPrice = unitPrice;
  }
 
  public double total() { return quantity * unitPrice; }
  public void addOne() { quantity++; }
  public String id() { return id; }
}

private keeps the field invisible from outside; final says it can never be reassigned after the constructor sets it. total() is a method that reads two fields and returns a value. addOne() mutates state. Anyone outside the class can call total() and addOne(), but no one can directly poke at quantity.

Creating Objects with new

You build an object by writing new followed by a constructor call:

javajava
Order o = new Order("A1", "Latte", 2, 5.50);
System.out.println(o.total());   // 11.0
o.addOne();
System.out.println(o.total());   // 16.5

Behind that one line, the JVM does several things: allocates space on the heap big enough for an Order, runs the constructor to fill in the fields, and gives you back a reference (essentially a pointer) you store in the variable o. When o goes out of scope and no one else references it, the garbage collector eventually reclaims that memory. You never call delete.

Encapsulation in One Sentence

Encapsulation is the practice of hiding fields behind methods so the class controls how its data changes. In the example above, you cannot set quantity to negative numbers from outside; you can only call addOne(). That makes the class easy to reason about, easy to refactor, and harder to break by accident. It is the single most useful idea in object-oriented programming.

When to Use a record Instead

Since Java 16 the language has had records, a one-line way to declare an immutable value class. If your "class" is mostly a data carrier with no real behaviour, use a record:

javajava
public record Customer(String id, String name, String email) {}

That gives you the constructor, equals, hashCode, toString, and read-only accessors (customer.name()) for free. Use a record for value objects (DTOs, query results, configuration). Use a normal class when you need mutable state or significant behaviour.

Static vs Instance: One Quick Rule

Anything marked static belongs to the class itself, not to any single object. Math.sqrt(2) is a static call: there is no Math instance involved. Use static for utility functions and shared constants, use instance methods (the default) for anything that depends on a specific object's state.

Where Java Goes from Here for You

You now have the two most important concepts in object-oriented Java. The natural next steps are inheritance and interfaces (how classes relate to each other), collections (List, Map, Set), and a small project — a CLI ordering system, a Spring Boot REST API, or a quick Android screen — to put it all together.

Common Mistakes Beginners Make

  • Making every field public. That gives up encapsulation. Default to private and expose methods instead.
  • Writing a constructor that does heavy work (network calls, database queries). Constructors should only initialise fields.
  • Forgetting that object variables are references, not copies. Passing an object to a method that mutates it will be visible to everyone holding the same reference.
  • Comparing strings with ==. That compares references. Use .equals() for content comparison.
  • Reaching for inheritance too early. Composition (one object holding another) is almost always simpler than extends.

Quick Reference

  • Declare a class: public class Name { ... } in Name.java
  • Declare a record: public record Name(Type field, ...) {}
  • Build an object: new Name(args)
  • Mark fields private final by default; relax only when needed
  • Use static for things that do not depend on any one object
  • .equals() for value comparison, == only for "are these the exact same object?"
  • IDEs like IntelliJ generate getters, equals, and hashCode for you — let them
Rune AI

Rune AI

Key Insights

  • A class is a blueprint; an object is one instance of that blueprint, allocated on the heap by new.
  • Encapsulate state by making fields private final and exposing methods instead.
  • Use records for plain data and classes for behaviour or mutable state.
  • Object variables are references, so mutations are visible to everyone holding the reference.
  • Compare strings and value objects with .equals(), never ==.
RunePowered by Rune AI

Frequently Asked Questions

What is the difference between a class and an object?

class is the blueprint, an object is one concrete instance built from that blueprint. You can have one class and a million objects of it.

Should I use `record` or `class`?

Use `record` for plain data carriers (immutable, no real behaviour). Use a normal `class` when you need mutable state, custom logic, or a class hierarchy.

Why is everything `private`?

So the class can change its internal storage later without breaking every caller. If the field is private, only the class itself touches it; the rest of the world goes through methods.

What is the difference between a static method and an instance method?

static method belongs to the class and does not need an object. An instance method runs on a specific object and can read its fields with `this`.

Where do objects live in memory?

On the heap, managed by the JVM. References to them live on the stack (local variables) or in other objects' fields. The garbage collector reclaims heap objects when no references to them remain.

Conclusion

Classes and objects are the unit of design in Java. A class describes the shape and behaviour, an object is one instance with its own state, and new is how you go from one to the other. Build the Order example, add a Customer record, write a tiny OrderBook class that holds a list of orders, and you will have done in twenty lines what entire frameworks are built on.