• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Make a class not to inherit and instantiate

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to make a class not to inherit and not to instantiate simultaneously
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you think about marking all the constructors in that class as private?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... 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.
 
Sheriff
Posts: 22798
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22798
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah, I forgot about that requirement.
 
ashok bantu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Not to inherit and instantiate any where whether it is inside or outside
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a private no-args constructor that throws an exception.
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Write a private no-args constructor that throws an exception.

Like this?
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Presumably any kind of private constructor would do? With or without arguments.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make the class final and the constructors private, then you are good to go
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AD: welcome to the Ranch

The final declaration is redundant, but still useful to alert users to the situation.
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The final declaration is redundant, but still useful to alert users to the situation.


Because of anonymous classes, final is not redundant.

This code will work and run as is. However, if you declare class Foo as final, it will not compile -- line 23 will cause a compiler error.

The final on line 22 is redundant though since without it, customMessage is effectively final.
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that was an omission of mine. Thank you. That will only happen, I presume, if the anynmous class is a nested clas in the first class.
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, that was an omission of mine. Thank you. That will only happen, I presume, if the anynmous class is a nested clas in the first class


If you have a private constructor, then yes, the anonymous subclass must be created from inside the class itself.

The private / final combo covers all the bases for preventing any instantiation from anywhere. The private constructor will restrict instantiation from outside of the class. The final will restrict subclassing, which covers instantiating an anonymous subclass. If you want to prevent any instantiation at all, then you should probably do this:


This should make the design intent clear.
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you I think I have suggested throwing such an exception somewhere myself.
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not at all. Little things like that are easy to overlook. It's a good reminder for me, too, because I'll often just have a private constructor and think my intent is clear.
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which case there should be at least a comment like
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic