Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private constructor

 
Mihai Lita
Ranch Hand
Posts: 36
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I`m reading from the K&B SCJP Exam book, and i've stumble upon the next rule for constructors:
- if the constructor has private modifier , if we want to allow an instance of the class to be used, we must provide a static method or variable that allows access to an instance created from within the class.
Why does it have to be a static method?
Can anyone help me with a link or provide a small example.
Thank you,
Mihai Lita
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it doesn't have to be a static method. But what if it isn't? Then you need an instance of the class so you can call that method on the instance.

So, now to create an instance of the class you have to have another instance of the class first. Where is that other instance going to come from?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its has to be static because- Static fields dont require an instance of the class and with private constructor once cannot create an instance of the class. Search for Singleton in Java- You will get a lot of queries in this forum or on the search as well.
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mihai Lita wrote:Hello,
I`m reading from the K&B SCJP Exam book, and i've stumble upon the next rule for constructors:
- if the constructor has private modifier , if we want to allow an instance of the class to be used, we must provide a static method or variable that allows access to an instance created from within the class.
Why does it have to be a static method?
Can anyone help me with a link or provide a small example.
Thank you,
Mihai Lita



With respect to the qoute "we must provide a static method or variable that allows access to an instance created from within the class" it is right wrt the SCJP Exam book, but java also allows to access the private constructor the other way round. Consider the following code. It is self explanatory.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True however using reflection has many disadvantages and is usually considered a bad practise (of course there are exceptions). It is certainly not something you should use in this situation and reflection is not a part of SCJP. The things Paul said were probably to make the poster think about the problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic