Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Singleton OCPJP 7

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any requirement imposed by singleton pattern to make it's instance accessible?
For e.g.

there is no way that we can have access to the instance of this class. Is it valid Singleton from exam point of view?
 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hamada,

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):

    Something like:
    Foo@7f31245a
    Foo@7f31245a

    Cheers!
    Chris

    BTW, congratulations on your OCA 7 91% mark - that's awesome!
     
    hamada yam
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Chris,
    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?
     
    author & internet detective
    Posts: 40797
    829
    Eclipse IDE VI Editor Java
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Chris Barrett
    Bartender
    Posts: 322
    24
    Eclipse IDE Firefox Browser
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for clarifying Jeanne!

    Mala had emailed me last night regarding a point I made on the OCA book, so I've sent her a separate email about this thread, too.

    Cheers!
    Chris
     
    hamada yam
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jeanne Boyarsky wrote:
    Without any static methods, the singleton is useless.



    if no static method then singleton is useless == invalid singleton , right ?
     
    Ranch Hand
    Posts: 1183
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    hamada yam wrote:
    if no static method then singleton is useless == invalid singleton , right ?


    Useless is probably a better description than invalid ; - )

    A very cute article at Navigate the deceptively simple Singleton pattern.

    Regards,
    Dan
     
    hamada yam
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dan Drillich wrote:
    Useless is probably a better description than invalid ; - )

    A very cute article at Navigate the deceptively simple Singleton pattern.



    Thanks , so for exam useless is acceptable ;) but not invalid .
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic