Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Constructor trap

 
imran anwar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 498
Eclipse IDE Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
 
imran anwar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is Math class's constructor private?
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic