Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton class  RSS feed

 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to extend a singleton class?
If so how can we insure that only one instance will exists?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the conventional Java "private constructor plus static getter" design you can't. The private constructor stops you. You can use some other mechanism to control instance creation and then use subclassing. Fer instance ...

I think there are some here who would consider this inner class idea A Good Thing. I get the feeling it's more like a trick answer to a trick question. Anybody? Bueller?
[ January 02, 2007: Message edited by: Stan James ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think only 'final' modifier stops you from extending a class right? A private constructor class can still be extended as an inner class, yes?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heh heh, yup. Interesting. Not sure how useful - probably more so than I imagine right now - but interesting.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Heh heh, yup. Interesting. Not sure how useful - probably more so than I imagine right now - but interesting.


JavaRanch's favorite promoter of functional languages uses them in this blog entry on mimicking Haskell's Maybe monad type in Java.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!