This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why can't create an instance from Math class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why can Watch "why can New topic

why can't create an instance from Math class


Joined: Jun 24, 2000
Posts: 14
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
public final class WhyMath {
public static void aMethod() {}
public static void bMethod() {}
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
The given answers are b & c.
More explanation is needed on this.
Help us.
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
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.



Joined: Jun 24, 2000
Posts: 14
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
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).]

Joined: Jun 24, 2000
Posts: 14
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
Yes Exactly! U won't see any private member of a class in API documentation.

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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
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
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

Joined: Jun 28, 2000
Posts: 93
Hi all,
Thanks a lot,that clarified my doubts.
I agree. Here's the link:
subject: why can't create an instance from Math class
Similar Threads
about Math class
Why can't I create a instance of Math class?
overriding / hiding a final static method
Define constants !