r/java • • Aug 03 '25

Teach Me the Craziest, Most Useful Java Features — NOT the Basic Stuff

I want to know the WILD, INSANELY PRACTICAL, "how the hell did I not know this earlier?" kind of Java stuff that only real devs who've been through production hell know.

Like I didn't know about modules recently

393 Upvotes

279 comments sorted by

View all comments

1

u/SpicyRock70 Aug 03 '25 edited Aug 03 '25

01- Implement a threadsafe, lazy-loaded singleton without using synchronization:

public final class Singleton {

    private Singleton() {}

    public static Singleton getInstance() {
        return Inner.INSTANCE;
    }

    private static class Inner {
        private static final Singleton INSTANCE = new Singleton();
    }
}

02- The SPI (service provider interface - look this one up) 03- Lexers like Jflex to create language parsers

2

u/Zardoz84 Aug 04 '25

Why this better that the enum based singletons ?

1

u/SpicyRock70 Aug 16 '25

Its not better or worse per se. I like it because I've been doing since before enums were in Java, and it feels like its misusing the spirit of the enum.