aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about Math class" Watch "about Math class" New topic
Author

about Math class

rainbow
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();
System.out.println(m.abs(2.6);
}
}
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.
Geek
Greenhorn

Joined: Sep 09, 2000
Posts: 21
Hi,
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.
Geek

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


geek@mantraonline.com
rainbow
Ranch Hand

Joined: Sep 10, 2000
Posts: 31
Greek:
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.
Rainbow
Anonymous
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.
fl
rainbow
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: http://aspose.com/file-tools
 
subject: about Math class