aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Math" Watch "Math" New topic
Author

Math

Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
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
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
Im just learning, but I bet the math class has a private constructor
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Jenny Yin:
I don't understand why java.lang.Math class doesn't hava a constructor.

Looking at the code for java.lang.Math reveals that it does indeed have a constructor (it's not possible for a class to not have a constructor (unless it's abstract, maybe)).
private Math() {}
I know I don't need to instantiate it to use its methods.

In fact, because the only constructor is private and no method is avaiable to *access* the private constructor, you can't instantiate this class. And that's why:
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."

Are things making sense?
[ March 14, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Dirk Schreckmann:
Looking at the code for java.lang.Math reveals that it does indeed have a constructor (it's not possible for a class to not have a constructor).

Actually, abstract classes do not have constructors.
Corey
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Corey,
all classes have at least the default no-arg constructor, even abstract class...
try the thing with javap and see for yourself
have a look at this post: http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=015127
[ March 14, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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

I thought that's what I said.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Math