• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Private constructor

 
Ranch Hand
Posts: 36
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic