Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing private constructor from main() (Java OCA 8 Programmer I Study Guide, Sybex)

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI guys, on the OCA book p 225 question n18 asks which of the following are true and option G says "You can access a private constructor with the main() method".
The solution on page 345 says that option G is actually true, and I don't understand how that could be. If main access the constructor it does it by declaring a new object which will invoke the constructor, say something like which will result in a compilation error if the constructor is private, at least according to p 198 where you can see the class Rabbit4 example.
Any idea?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Any idea?

Of course!

It all depends on where the main() method is defined. If it is defined within the class defining the private constructor, you can invoke the private constructor without any problem from within the main() method. And that makes sense as you can invoke/access any private member from within the class defining this member and the main() method is defined within this class. So the compiler is a happy camper!
But if the main() method is defined in another class than the class defining the private constructor, the compiler is not happy and will complain about invoking the private constructor. So you'll get a compiler error.

Here is a code snippet illustrating both scenarios

Hope it helps!
Kind regards,
Roel
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 345 answer of question 18. It says
OCA 8 sybex wrote:Since the main() method is in the same class, it can call private methods in the class.
I think It meant to say private constructors, Is that an errata?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:I think It meant to say private constructors, Is that an errata?

That statement in the explanation is definitely true! If the main() method is within the class, it can call private methods. But because this review question is about "private constructors", it might probably be better to use "private constructors" as well. Or maybe it could be changed to "it can access private members in the class". That covers everything
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes as question is about constructor generally users might expect constructor. Yes private members would cover all
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK got it, thank for the explanation, it's clear now!
cheers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic