• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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: 17734
302
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
reply
    Bookmark Topic Watch Topic
  • New Topic