aspose file tools*
The moose likes Beginning Java and the fly likes Make a class not to inherit and instantiate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Make a class not to inherit and instantiate" Watch "Make a class not to inherit and instantiate" New topic
Author

Make a class not to inherit and instantiate

ashok bantu
Greenhorn

Joined: Jan 30, 2009
Posts: 10
how to make a class not to inherit and not to instantiate simultaneously
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

what do you think about marking all the constructors in that class as private?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14156
    
  19

What do you mean exactly? You want to make a class that cannot be subclassed and that cannot be instantiated?

To make it impossible to subclass a class, make the class final.
To make it impossible to instantiate a class (from outside the class itself), make all constructors of the class private.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Jesper de Jong wrote:
To make it impossible to instantiate a class (from outside the class itself), make all constructors of the class private.

even the class cant subclass in this case.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
... you cannot extend from a class with only private constructors, but it is best to mark the class final so any users know it cannot be extended from. That class should only have static members. There is a section about it in the Java™ Language Specification.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Campbell Ritchie wrote:That class should only have static members.

Only if it's meant to be a utility class. If the class is a singleton class, or if you provide static factory methods for creating instances, then having non-static members is allowed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
If it is a singleton, or has factory methods, then it can be instantiated, albeit only locally to its own class. It said "not to ... instantiate" in the thread title. So I presumed it must be a utility class with static members only, like java.lang.Math, which only has a private constructor.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

ah, I forgot about that requirement.
ashok bantu
Greenhorn

Joined: Jan 30, 2009
Posts: 10
Hi guys,

Not to inherit and instantiate any where whether it is inside or outside
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4546
    
    5

Write a private no-args constructor that throws an exception.


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Darryl Burke wrote:Write a private no-args constructor that throws an exception.
Like this?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Presumably any kind of private constructor would do? With or without arguments.
Ramakrishna Gummadi
Greenhorn

Joined: Jun 29, 2011
Posts: 16
ashok bantu wrote:how to make a class not to inherit and not to instantiate simultaneously


declare a class's constructor as private then you cant inherit it and cant create object to it.
so that class cannot be inherited and cannot be instantiated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Make a class not to inherit and instantiate