java - What is the purpose of a private constuctor with no parameters? -
i trying practice code analyzing , have noticed there constructor no parameters (which private) in below code snippet.what purpose behind whole code?
public class wit { public static wit instance = null; private wit() { } public static wit getinstance() { if (instance ==null){ instance = new wit(); } return instance; } }
this following singleton pattern, makes sure class can have 1 instance making default constructor private , handling instantiation internally.
in example, however, singleton guarantee broken exposing non-final instance variable. can come along , set reference null, causing instance created on next call getinstance().
Comments
Post a Comment