What is the benefit of using the Singleton pattern like this:

public static ConfigManager getInstance() {
return ConfigManagerHolder.INSTANCE;
}

private static class ConfigManagerHolder {

private static final ConfigManager INSTANCE = new ConfigManager();
}
}
public static ConfigManager getInstance() {
return ConfigManagerHolder.INSTANCE;
}

private static class ConfigManagerHolder {

private static final ConfigManager INSTANCE = new ConfigManager();
}
}
What is the benefit of having an internal class compared to just returning a new ConfigManager()
17 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @userexit! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
userexit
userexitOP7mo ago
so like in the first class have private static final ConfigManager INSTANCE; then inside getInstance() if null then create new else return current
nikcho-kouhai
nikcho-kouhai7mo ago
I don't really think there is a benefit to this seems pretty pointless
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
nikcho-kouhai
nikcho-kouhai7mo ago
well it's fine if you aren't using multiple threads anyway
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
nikcho-kouhai
nikcho-kouhai7mo ago
(by lazy he means that the object isn't loaded when the object is needed, but is instead loaded anyway which is also true)
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
how would you adapt this code to be an enum ?
nikcho-kouhai
nikcho-kouhai7mo ago
public enum Singleton{
INSTANCE;
private Singleton() {

}
}
public enum Singleton{
INSTANCE;
private Singleton() {

}
}
userexit
userexitOP7mo ago
what does the constructor do
nikcho-kouhai
nikcho-kouhai7mo ago
well nothing in this case it just constructs the object you can remove the constructor in this case and a thing to note as borgel said generally speaking if you have to use a singleton you should probably restructure your code
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
its just to load properties files that i will use across my program
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
userexit
userexitOP7mo ago
i do Properties instance no ? Properties properties = new Properties(); properties.load(fileInputStream) properties.getProperty(String key)...
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server