I am studying the idea reference counting for shared objects in Java. This piece of code threw me for a loop:

class Shared {
private int refcount = 0;
private static long counter = 0;
private final long id = counter++;

It’s laughable, but I couldn’t figure out how the counter kept counting as I created new objects, challenging my understanding of Java and OOP. Such a simple piece of code seeming so clever and (pardon the word) cute.

Advertisements