• 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 can't create an instance from Math class

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The definition of Math is:
public final class Math
I can't figure why can't create an instance from this class.
For example, following class can be instantiated with no
problems.
public final class WhyMath {
public static void aMethod() {}
public static void bMethod() {}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi JavaMiller,
Math class has "STATIC METHODS" and so you can not instantiate. But I too need more explanation,When I took Exam Cram CD first exam I got the question on this.
Which of the follwing are used to create Immutable objects.
a)java.lang.Math
b)java.lang.Long
c)java.lang.Float
d)java.lang.StringBuffer
The given answers are b & c.
More explanation is needed on this.
Help us.
Thanks
Nirmal
 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Math Class have Private constructor that is why u can't instantiated.
Nirmal all the wrapper classes and String class creates Immutable objects.
Any feel free to correct me.

vivek

 
javamiller
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class definition given at the end of original question
contains and contains only static methods. It can be
instantiated. Why Math class can't?
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the source file for Math:
public final class Math {
/** * Don't let anyone instantiate this class.
*/
private Math() {}
The reason you can't instantiate it is because it has a private constructor. This is a good example of a good use for a private constructor - Make a public static method - like "createInstance()" check a static class int, if greater than 0, do not create an instance but return a reference to the current one, if == 0 call the private constructor and create an instance. In this way, you can allow one and only one instance of a class to be in existence at a time.
[This message has been edited by Paul Smiley (edited June 28, 2000).]
 
javamiller
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vivek Shrivastava,
I am wondering why there is no mention of constructor(s)
in Math API documention. It is because they are private.
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Exactly! U won't see any private member of a class in API documentation.

vivek
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul- you're thinking of the Singleton pattern, in which you want one and only one instance of a class to exist. But Math doesn't even have that - there is no static getInstance() method to access the private constructor. You can never even get a single instantiation - Math is completely uninstanitable.
Nirmal- the reason Math is left of the list of immutable objects, is that you can never have a Math object in the first place. Being immutable is meaningless if the object can't even exist.
Several people have explained why, in Java terms, it's impossible to instantiate Math. The other part of the question is, why did the Java language designers set up Math this way? Remember that it takes a certain amount of time and memory to create any object, so it's worthwhile to not create an object unless there's a use for it. In designing the Math class, they had a large number of methods which had no need to use any instance variables - i.e. no private data, and no need for a "this" reference anywhere in the code. So these methods might as well be made static. (Static methods, like final methods and private methods, are slightly faster to execute because there's no time spent doing dynamic lookup to check for overriding methods.) Once the methods are all static there's no need for any Math instance. And once there's no need for any Math instance, they might as well prevent anyone from wasting time and memory creating Math objects which have no possible use. So that's what they did.
 
Paul Smiley
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim,
I agree with you wholeheartedly. I was just trying to explain why one would want to use a private constructor, not to say that it's used in this instance. But I believe that there are some Singleton's in the JDK - I'm thinking of the Calendar class in particular. And the instance is created by the class loader of the JVM, is it not?
Before I said to look at the javadocs - I was wrong of course! I meant look at the source file for java.lang.Math.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Runtime is a singleton class in the jdk. The JVM creates an instance of Runtime when it first starts, and using the static getRuntime() method of the Runtime class you can obtain an instance of Runtime.
Infact if you simply want to prevent people from creating an instance of your class, you should declare the constructor private, instead of unnecessarily declaring the class abstract.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Thanks a lot,that clarified my doubts.
Regards.
Nirmal
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First,you cannot make an instance of the class Math,because it has only a single constructor and it's been marked private and you just can't make an instance  of it from outside the class.

 
 Second,you don't need to do that.All of the methods in class Math are static,just use the class name and the dot operator and you can invoke any one of them.
e.jpg
[Thumbnail for e.jpg]
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whew!  I'm so glad this has finally been resolved.  
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic