File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Private Constructor trap

imran anwar

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?
Marcelo Sousa Ancelmo
Ranch Hand

Joined: Jan 17, 2002
Posts: 498

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

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);
I agree. Here's the link:
subject: Private Constructor trap
It's not a secret anymore!