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 about Math class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about Math class" Watch "about Math class" New topic

about Math class

Ranch Hand

Joined: Sep 10, 2000
Posts: 31
Which of the following statements are true about the fragment below?
import java.lang.Math;
public class Test {
public static void main(String args[]) {
Math m = new Math();
A) Compiler fails at lin 1
B) Compiler fails at line 2
C) Compiler fails at the time of Math class instantiation
D) Compiler succeeds.
why can't Math class be instanced?
Math is public final class.It's no reasonable that Math class can't be instanced.

Joined: Sep 09, 2000
Posts: 21
The compiler fails at line 4 because it is unable to create the instance of the Math Class, as its constructor is private, so cannot be accessed. But as all the methods in java.lang.Math are static, you dont need to create any instance of the math class to use them.

[This message has been edited by Geek (edited September 14, 2000).]
Ranch Hand

Joined: Sep 10, 2000
Posts: 31
Why do you think the Math class constructor is private.I can't find the constructor description in java 2 specification document.
Normally if there is no explicit constructor,the system will provide a default constructor for the class.And the default constructor's access modifier is same as the class.i.e. a public class will have a public default constructor.Math class is public.So I think its constructor is also public.
if my opinion is wrong.pls correct it.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Try typing: javap -private java.lang.Math on the command line. This will show all the methods.
Ranch Hand

Joined: Sep 10, 2000
Posts: 31
Geek,you are right.
the constructor of Math is private.So Math can't be instanced.
Thanks all.
I agree. Here's the link:
subject: about Math class
It's not a secret anymore!