This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about Math class

 
rainbow
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
rainbow
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Try typing: javap -private java.lang.Math on the command line. This will show all the methods.
fl
 
rainbow
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Geek,you are right.
the constructor of Math is private.So Math can't be instanced.
Thanks all.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic