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

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -