Help coderanch get a
new server
by contributing to the fundraiser
  • 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

why I can't instance Math class?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class test
{
public static void main(String arg[])
{
Math m=new Math();
System.out.println("m.round="+m.round(100.00));
}
}

when compile the code ,the code give me a compile error:there isn't a construcor in class Math,but I look up in the jdk-doc,the Math is subclass of the class Object,the Object have a no-arg constructor : public Object()
,which mean that when instance the Math class,it will invoke the super class(Class)'s no-arg constructor : public Object().
If i am right about the question,how can I explain this.
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right about the Math class constructor invoking its superclass' (Object) constructor. However, Math's constructor is private so it cannot be instantiated from outside of the Math class itself. But then again, there is no need to instantiate Math because all of its methods are static. So really, the only reason that Math's constructor is there in the first place is so that it can be made private (otherwise, the compiler will provide a default public no-args constructor since Math is public).
In addition, Math is a final class so you cannot subclass it.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic