File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes class Math is concrete class but still it cannot be instantiated. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "class Math is concrete class but still it cannot be instantiated." Watch "class Math is concrete class but still it cannot be instantiated." New topic
Author

class Math is concrete class but still it cannot be instantiated.

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
I was trying Math m = new Math(); and later found that Math constructor is private and it does not have any other constructors as well. I understand that all the methods in Math are static but i was just trying my SCJP knowledge.
Here is the code of class Math taken from Sun Java 1.5



In real world if requirement is to write a Utility class that has only static methods then is the above behavior of making the constructor private done just to avoid instantiation of the class inorder to save memory? Or is there any other significance behind this?

Thanks
Deepak


SCJP, SCWCD, SCBCD
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Making the constructor private is a common trick for utility classes that contain only static methods and that should not be instantiated or subclassed.

Note that making the constructor private also makes it impossible to make a subclass, because the constructor of the subclass needs to be able to access the constructor of the superclass, and it can't if the constructor of the superclass is private.

I wouldn't say this is just "to save memory". Utility classes such as Math are not supposed to be instantiated. Calling static methods via an instance reference is bad style. Making it impossible to instantiate a class prevents users from using that bad style.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Joshua Fix
Ranch Hand

Joined: Sep 18, 2007
Posts: 57
Why not make the class abstract? What is the difference between an abstract class and a class with a private constructor?


SCJP 5.0
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Making the class abstract still allows someone to extend the class and then instantiate that subclass. A private constructor prevents instantiation of both the class itself and any subclasses.

But note that Math is also declared to be final, to prevent it from being subclassed at all. Unfortunately, you can't declare a class as both final and abstract at the same time, so it still needs to define the private constructor to prevent instantiation.


SCJP 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class Math is concrete class but still it cannot be instantiated.