File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why can't create an instance from Math class

 
javamiller
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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() {}
}
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • 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
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Smiley
Ranch Hand
Posts: 244
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Exactly! U won't see any private member of a class in API documentation.

vivek
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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: 244
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Nirmala
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Thanks a lot,that clarified my doubts.
Regards.
Nirmal
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic