This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I don't understand why java.lang.Math class doesn't hava a constructor. I know I don't need to instantiate it to use its methods. But one of the test questions in RHE asks about it. When I tried to create an object from Math class using new Math(), I got the error: "Math() has private access in java.lang.Math." Thanks, Jenny
Actually, the Math class does have a constructor - it's just private. That means that you can never instantiate an instance of a Math object. That can only be done by the Math class itself. Of course, no method within the Math class does that, so I guess you can never actually create an object of type Math. The Math class isn't made to need an instance, though. Every method is static, so you don't need an instance to invoke them. If they would have declared Math to be abstract, then Math really wouldn't have a constructor as you can't instantiate an abstract class. However, they made the Math class final, instead. You can't have a final abstract class, so the only option left to disallow people making instances of the class is to make the constructor private. Math is built the way it is for a reason. It has no state. Therefore, you never need to create an instance of it. I hope that helps, Corey
Of course they do. I'm an idiot. An abstract class, although, uninstantiable, can still have state information because that state information may be inherited by any subclasses. Whenever a subclass of an abstract class is instantiated, a call must be made to a constructor of the parent class (in this case, an abstract class) in order to ensure that all inherited members have a chance to be initialized properly. I don't know what I was thinking. Sorry about that. Corey
Joined: Aug 26, 2001
can still have state information because that state information may be inherited by any subclasses. Actually, the abstract class itself can also have state information (instance variables). Sorry to nitpick
Joined: Dec 20, 2001
Originally posted by Valentin Crettaz: can still have state information because that state information may be inherited by any subclasses. Actually, the abstract class itself can also have state information (instance variables). Sorry to nitpick