• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about Math class

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Geek,you are right.
the constructor of Math is private.So Math can't be instanced.
Thanks all.
 
reply
    Bookmark Topic Watch Topic
  • New Topic