This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private Constructor trap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private Constructor trap" Watch "Private Constructor trap" New topic
Author

Private Constructor trap

imran anwar
Greenhorn

Joined: May 06, 2002
Posts: 12
hi all...
what is the 'private constructor' trap that Ajith refers to, in his potential traps list?
are there some specific conditions under which a private constructor is not supposed to be used?
thanks
Marcelo Sousa Ancelmo
Ranch Hand

Joined: Jan 17, 2002
Posts: 497

Hi imran
Whit a private construtor you can't make an instance of the Class in another class, I think that the best example about the private construtor is in Math Class, the construtor of Math is private, so you can't do:
public class Test {
Math myMath = new Math();
}


Marcelo Sousa Ancelmo - Brazil
imran anwar
Greenhorn

Joined: May 06, 2002
Posts: 12
why is Math class's constructor private?
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
becauase sun doesnt want you to make an instance of the class Math. so you cant do this:
Math x=new Math(); // Wrong!!
you dont need to do that because all the Math methods/fields are STATIC. thats means you can sue the class name to access them without creating an instance of that class.

like Math.round(5.5);
 
GeeCON Prague 2014
 
subject: Private Constructor trap