// Created on savesnippets.com ยท https://savesnippets.com/wprGoA4uPEIsaj public enum Config { INSTANCE; private final Properties props = new Properties(); Config() { // Constructor is called exactly once, by the JVM try (var in = ClassLoader.getSystemResourceAsStream("config.properties")) { if (in != null) props.load(in); } catch (IOException e) { throw new RuntimeException("failed to load config", e); } } public String get(String key) { return props.getProperty(key); } public int getInt(String key, int def) { var v = props.getProperty(key); return v == null ? def : Integer.parseInt(v); } } // Usage from anywhere class App { public static void main(String[] args) { var dbUrl = Config.INSTANCE.get("db.url"); var port = Config.INSTANCE.getInt("server.port", 8080); System.out.println(dbUrl + " : " + port); } } // Why enum is the best singleton: // - Lazy initialization: enum constants are constructed on first reference // - Thread-safe: JVM enforces this for enum class initialization // - Serialization-safe: enum constants survive serialization round-trip // - Reflection-safe: cannot use newInstance() to create another import java.io.IOException; import java.util.Properties;