Fundamentals 9 min read

Understanding Java Classes, Objects, and the JVM: From Basics to Garbage Collection

This article introduces Java classes as object factories, demonstrates how to define and instantiate classes with fields and methods, explains the JVM architecture and memory areas, and explores garbage collection algorithms, providing code examples and conceptual analogies for beginners.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding Java Classes, Objects, and the JVM: From Basics to Garbage Collection

Hello everyone, I am a Java class, delighted to share my origin and role in object‑oriented programming.

In Java, everything is an object, and objects are created from classes, much like products are molded from a template.

A minimal class definition looks like this:

class JavaClass {
}

Instantiating the class is straightforward using the new keyword: JavaClass javaClass = new JavaClass(); A useful class should contain fields and methods; for example, a Person class with age, height, hobbies, and actions such as walk, run, and talk:

public class Person {
    private int age;
    private int height;
    private String hobbies;
    public void walk() {
        System.out.println("I can walk!");
    }
    public void run() {
        System.out.println("I can run!");
    }
    public void talk() {
        System.out.println("I can talk!");
    }
}

A simple HelloWorld program creates a Person object, prints "Hello World", and calls walk:

public class HelloWorld {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println("Hello World");
        person.walk();
    }
}

The program outputs "Hello World" followed by "I can walk!", illustrating object creation, method invocation, and console output.

Beyond the surface, Java achieves cross‑platform execution through the Java Virtual Machine (JVM), whose architecture includes a class‑loader subsystem, runtime data areas, and an execution engine.

After compiling source files with javac, the resulting .class files contain bytecode that the JVM interprets; tools like javap can display this bytecode.

The JVM employs three class loaders—Bootstrap, Extension, and System—and allows custom loaders, all following the parent‑delegation model.

Memory within the JVM is divided into several regions: the Method Area (stores class metadata), the Heap (where objects reside), the Virtual Machine Stack (holds stack frames with local variables), the Program Counter (tracks execution), and the Native Method Stack (supports native code).

The Heap is organized generationally into Young Generation (Eden, Survivor spaces) and Old Generation, with a separate space for class metadata (Metaspace in JDK 8+).

Garbage Collection (GC) reclaims unreachable objects; the author humorously suggests calling them "Retired Objects" instead of "Garbage" and notes that GC may pause all other threads (stop‑the‑world) to perform collection.

Common GC algorithms include Reference Counting, Mark‑Sweep, Copying, Mark‑Compact, Incremental Collecting, and Generational Collecting.

Typical collectors are Serial, ParNew, Parallel Scavenge, CMS, among others, selectable via JVM options.

In conclusion, the Java class narrates its purpose: to serve as a blueprint for objects, enabling developers to build functional programs while the JVM manages execution and memory.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMGarbage CollectionclassesObject‑Oriented
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.