This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
Joined: Nov 22, 2008
Posts: 18944
posted
0
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
Joined: Jun 03, 2000
Posts: 277
posted
0
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
Joined: Jun 24, 2000
Posts: 14
posted
0
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
Joined: Jun 02, 2000
Posts: 244
posted
0
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
Joined: Jun 24, 2000
Posts: 14
posted
0
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
Joined: Jun 03, 2000
Posts: 277
posted
0
Yes Exactly! U won't see any private member of a class in API documentation.
vivek
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
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.
"I'm not back." - Bill Harding, Twister
Paul Smiley
Ranch Hand
Joined: Jun 02, 2000
Posts: 244
posted
0
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
Joined: Jun 27, 2000
Posts: 213
posted
0
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
Joined: Jun 28, 2000
Posts: 93
posted
0
Hi all, Thanks a lot,that clarified my doubts. Regards. Nirmal