Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

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();

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.
Posts: 16921
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
Everybody's invited. Except this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    Bookmark Topic Watch Topic
  • New Topic