• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between private constructor and making class final ?  RSS feed

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some of classes like String,StringBuffer,Class are final classes and few of the classes are declared the private constructor like DriverManager,Collections .

so these above two thing are same.

1) Means class can't have the subclass
2) how it is related with singleton Class, because singleton class also have the private constrcutor and static Synch method.

Thanks in advance !
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making the constructor private makes the class effectively final because a the sub-class can't access it's constructor. This is used for the singleton pattern but also for builder and factory patters.

The singleton usually creates a static instance using the private constructor and provides a public static method to gain access to that instance.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes means i am correct.

also we can say DriverManager and Collections are the Singleton Patterns.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The DriverManager class is just a class that holds basic services for managing JDBC drivers.
The Collections class is just a utility class for collection related stuff.

If a class doesn't have a public/protected constructor that doesn't necessarily mean that it implements the singleton pattern.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the below code is singleton class:



it has private constructor. What else, is it code implemented in synchronized public static method getObjectReference() makes this class singleton ?

what do you think , am i correct ?

one more thing
and what about the Class class which has the below code:

/*
* Constructor. Only the Java Virtual Machine creates Class
* objects.
*/
private Class() {}


means it is singleton Class.

while it doesn't have the public static synchronized Class(){ }
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted is not really a singleton. You can create multiple copies of the single class (which should start with an uppercase letter) by resetting the value to null.
Also when 2 threads access getObjectReference() at the same time you can get multiple copies.

I would not learn the singleton pattern by looking at the Class class because it for fills a special role in Java but I would look at the Singleton_pattern wiki.
 
Puja Mantri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
making the constructor private means, class cannot be instantiated from outside. This is one of the ways to make a class singalton.

final class means any other class cannot extend that class and state of the variables of class does not change due to any operation on object of class after instantiating the class. Hence usually this class has parameterized constructors. Constructor of the final class need not be private, because final class can have multiple objects.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yap , making the private constructor() is the way to not allowed to create the instance of that class.

But for Singleton this is enough or we need to put the implementation method with "public static synchronized"

i am correct ?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you must ensure that there can only be one instance of the class.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a private constructor in a class, it is probably a good idea to mark that class final, so users are reminded of that.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's good practice to always declare a class final, regardless of whether its constructors are private.

Unless of course you intend the class to be overridden.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I think it's good practice to always declare a class final, regardless of whether its constructors are private.

Unless of course you intend the class to be overridden.
Which is no possible if the constructor is private. But I agree with you that you should make your class final in that case. I think it's clearer then a private constructor.
 
Paul Clapham
Sheriff
Posts: 22821
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you have a private constructor in a class, it is probably a good idea to mark that class final, so users are reminded of that.


That would only apply if all constructors were private; it's possible to have a combination of private constructors and public constructors, for example.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote: . . . combination of private constructors and public constructors, . . .
Good point. I forgot that bit. And agree with Stephan van Hulst about the desirability of all classes being final.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!