• Post Reply Bookmark Topic Watch Topic
  • New Topic

class which has private constructor?  RSS feed

 
Tanzeem Akhtar
Greenhorn
Posts: 9
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all...

Wel you people can you please help me out in the following scenario...

How to get an object of a class which has private constructor?
Please justify it.

Thanking you..
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not possible if you don't have a method that exposes the instance of that class. If it does, it's called singleton.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question, moving...

(And please, use a meaningful subject line)
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Freddy Wong:
Not possible if you don't have a method that exposes the instance of that class. If it does, it's called singleton.


No. A singleton is a class that guarentees there is only one instance in a particular JVM. The existance of a private constructor is not enough to guarentee that.

Also, you can instantiate classes with only private constructors via reflection.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you can instantiate classes with only private constructors via reflection.

OO yeah, I forgot about that. Thanks for pointing out
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!