Hi,
Java has four (4) levels of access to class members, private, default (this is when no modifer is supplied) protected and public.
Private is the must restrictive and public the must open.
For a deeper explanation of accessors see
HERE Ok, A singleton is a design
pattern, the pattern calls for only one instance of a class to exist during the execution of the application.
If a classes constructor is public it can be called in the normal ways and you can create as many instances of a given class as you wish.
If the constructor is private, then no instance of the class can be created from outside the class, because the compilor/JRE can not see that constructor. Remember private access means that only the class itself can see the member marked as private, and classes outside can not see a private memeber.
Now while you can use protected and default to provide a singleton, these accessors still allow classes in the same package and inherited classes to call teh constructor, so the to ensure total control the best is to use private.
I have explained teh code a bit this time:
Ok thats the singleton, the only way to get an instance is to call EgSingleton.getInstance();
The code above when ran should produce there lines of out put, the first saying tthe singleton was being created and then two lines with a nd b both being equal to teh same value. The value is the memory address. If you were to make the EgSingleton constructor protected or default
you should get the same results.
If you made the constructor public replace the lines with getInstance on them with the following and you will see that two different objects are created.