The point of the Singleton Pattern is to restrict instantiation of a class to a single object. Access is only provided through the static getInstance() method, which ensures that the class has only a single instance through the whole life of the application. Therefore, the code shouldn't allow direct access to the instance variable (which I think is what you mean).
I was always told the three basic requirements of a Singleton are:
private constructor to stop instantiation
private static variable to hold the reference to the single object and to stop direct access (Foo.instance)
public static method for getting access to that static variable
Now, one of those items is not present in your code. Can you see which one? Think about what happens when a static reference to a non-static method is attempted.
If all is good, a test like this should compile and generate the same memory address (as both fooSingleton1 and fooSingleton2 are accessing the same static object):
BTW, congratulations on your OCA 7 91% mark - that's awesome!
The code which i posted will compile ok, but as you mentioned there is no public static method for getting the static variable, So it is not valid Singleton but i am studying from Mala Gupta OCPJP 7 manning early edition_11 and there was a question that choose correct singleton like :
and She mentioned that all the answers(a,b,c,d) are correct although there is no public static instance method to the the instance. Could you please have a look why these answers are correct?
That looks like a simple typo. Keep in mind that MEAP books are still going through review. You should report your finding in the Manning book forum so they can fix it before the book publishes. You can even link to this thread to show you got independent confirmation. (and so Mala can post here confirming)
In answer to your literal question, it's still a singleton even if the instance isn't accessible. For example:
In this example, we return the state rather than the Foo object directly. Something is made available though. Without any static methods, the singleton is useless.