How Java works

Pick a topic, write code and watch what happens inside, step by step.

Memory

Collections

Concurrency

Language and JVM

Topics

Class loading

ClassLoaders and delegation

Three main ClassLoaders load classes. Each one asks its parent first, and searches itself only if the parent finds nothing.

This is why your own java.lang.String would not replace the one from the JDK.

Pick a class with the buttons and follow the request.

Three stages

A class is loaded, then linked, then initialized. Each happens once.

In Preparation, static fields get default values. The value you wrote arrives only in Initialization.

The JVM does not initialize a class until its first real use.

Static and constructor order

A favorite interview question: the static part first, parent to child. Then for each new, the instance part, again parent to child.

Inside a class, fields and blocks run in written order. The constructor body is last.

Follow how the output appears line by line.

A virtual method in a constructor

name is written as "Aysel", yet the first output is name=null.

The parent constructor calls a virtual method and Java picks the child version. At that moment the fields of the child are not set.

The rule: do not call overridable methods in a constructor.

When does initialization happen?

It takes real use: new, a static method, or a static field that is not a constant.

static final int X = 5 is a compile-time constant, copied at compile time. static final Integer Y is not a constant.

Using a parent field through the child name initializes only the parent. Creating an array initializes nothing.

Two traps

Static fields run in line order. When a reads c, c is still 0, not 10.

An exception in <clinit> breaks the class for good: ExceptionInInitializerError the first time, then NoClassDefFoundError every time.

The second error hides the real cause, so look for the first one in the log.