10

Having already read how to make a class Immutable by following steps

  1. Don't provide "setter" methods — methods that modify fields or objects referred to by fields.
  2. Make all fields final and private.
  3. Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.
  4. If the instance fields include references to mutable objects, don't allow those objects to be changed:
    a. Don't provide methods that modify the mutable objects.
    b. Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.

AM not sure I clearly understand the utility of private constructor and factory method in the context of immutability. If I make class final, basically am closing all paths of any other class extending it. How is the stated a more sophisticated approach

I have seen private constructor,factory method in Singleton pattern which makes sense. But when we talk of object immutability, are we also restricting object construction/instantiation when we mention private constructor and static factory methods??

HungryForKnowledge
  • 289
  • 1
  • 8
  • 18
  • Yes. Doing (2) and (4) should be enough to make the instances immutable. Doing (3) makes the class impossible to extend (or at least, any subclass would not be instantiable), which is entirely different. – Dawood ibn Kareem Nov 30 '12 at 17:08

8 Answers8

4

Firstly, there are several reasons why immutable class generally should not be overriden, you can find them here.

That said, making a constructor private is just one way to prevent class from being overriden. Why? Because in sub-class, every constructor (implicitly) calls super(), a default constructor of base class. But if you make this constructor private, sub-class cannot call it and thus cannot override the base class. This approach is very suitable when you want to control the total number of instances of particular class, for example in case of singletons.

Community
  • 1
  • 1
Miljen Mikic
  • 14,765
  • 8
  • 58
  • 66
  • If you make a constructor private in super class, there is no sense in making a subclass, is there?? because if you do have a subclass and your super class has a private constructor, its going to see its doom as a compile time error.. – HungryForKnowledge Nov 30 '12 at 17:20
  • But I do see a connection between immutability and non-extensibility..subclassing might break the purity of immutable..at least I could make out that from the link you posted – HungryForKnowledge Nov 30 '12 at 17:27
  • immutability is not linked with private constructors or factory methods. Many classes in JDK have both factory methods and public constructors. – Narendra Pathai Nov 30 '12 at 17:40
  • 1
    I think that if you have a requirement for immutability in a particular class, you need to think about whether that also applies to its subclasses. Should there be subclasses; if so, should any subclasses even be instantiable; and if so, should those instances be entirely immutable, immutable only with respect to those members that are in the base class, or not immutable at all? You need to decide this before you consider whether you need either a private constructor or a final declaration. – Dawood ibn Kareem Nov 30 '12 at 17:41
  • @HungryForKnowledge - exactly, not that there is no sense, there is no **way** to make a subclass and that's the purpose of private constructor in this case. Sub-classing can break the security of immutable class and that's basically the main reason of preventing it, but there are few more listed in that link. – Miljen Mikic Nov 30 '12 at 17:56
  • @DavidWallace I think I understand what you are saying..If we do need subclasses, we actually go with a public constructor and find other ways of making the subclass instances also immutable..else final..also can go with protected constructor for classes in same package to extend.. – HungryForKnowledge Nov 30 '12 at 17:57
  • @DavidWallace - usually, immutability implies "no subclassing". See the link I provided, however in some implementation (e.g. BigInteger) they forgot this request. – Miljen Mikic Nov 30 '12 at 17:58
  • See also this blog of Jon Skeet: http://msmvps.com/blogs/jon_skeet/archive/2008/01/26/immutability-and-inheritance.aspx, and the final sentence "Anyway, the broad point of this post (other than to hopefully relieve my own insomnia - and possibly yours too) is that immutability and inheritance don't mix terribly nicely.." – Miljen Mikic Nov 30 '12 at 18:00
  • Thanks for the link, @MiljenMikic. For my money, the best wisdom on that page is James Arendt's comment - "... immutable objects are essentially value objects ... should be sealed and not participate in inheritance." He's absolutely right, of course. Perhaps composition is a better design than inheritance, when you want the properties of one value class to be a superset of the properties of another. – Dawood ibn Kareem Dec 03 '12 at 07:50
  • @DavidWallace Although we can debate why would anyone want to override immutable class, I agree with you - composition is way to go. Take a glance at this thread: http://stackoverflow.com/questions/81786/can-i-add-new-methods-to-the-string-class-in-java, Aldo Barreras gave an example of extending the String class. – Miljen Mikic Dec 03 '12 at 08:48
  • i see your point in after having a private constructor, it cannot be extended. But same functionality is achieved by marking class as final. isn't it ? – Mateen May 27 '18 at 11:56
  • @Mateen Of course. As stated in the original question, having a private constructor is an *alternative* to marking the class final. It is not often used, but in certain cases it is useful. – Miljen Mikic May 28 '18 at 07:57
3

After thoroughly reading the below point

Dont allow subclass to over ride methods. The simplest way to do this is to declare the class final. A more sophisticated approach is to make constructor private and construct instances in factory method.

I think the point here is not

Making class final and private constructor.

The point is

Either you make your class final or have a private constructor.

Hope it helps!!

source

Mateen
  • 1,631
  • 1
  • 23
  • 27
2

I think the big issue here is future refactoring. Suppose, in a later version, you find it would make something much simpler if you could split out some new special case of MyClass into a subclass, MySpecialClass.

If MyClass were a mutable class with a public constructor, you could just do it and tell users of the new features to create a new MySpecialClass. Existing uses are not affected.

If MyClass has a private constructor and a factory method, there is no problem. You declare MySpecialClass nested in MyClass, also with a private constructor. Add and/or modify the factory methods to choose which to create, but make sure existing calls go on working compatibly.

What would you do if MyClass were immutable, and final, but had a public constructor?

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
1

Yeah, you are right. It does not make any sense to make constructor private. By doing this, we are restricting the instance creation, which is not a desired scenario for immutability.

In example mentioned in sun site does not make constructor private http://docs.oracle.com/javase/tutorial/essential/concurrency/syncrgb.html

Vishal
  • 3,189
  • 1
  • 15
  • 18
  • This is a Synchronized Class example..I think u meant http://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html – HungryForKnowledge Nov 30 '12 at 17:24
  • 1
    Right... Though the class is immutable, but they did not reduce the visibility of the constructor. I think making constructor private will increase the overhead in the code. – Vishal Nov 30 '12 at 17:26
1

IMMUTABILITY - Very helpful for concurrency as it avoids creation of various invariant that are possible in threading environment.

FACTORY METHODS - Just a naming convention, as they are more verbose to read and easy to understand from their custom name. For example: copyOf() method would make more sense than creating a copy constructor. As said by Joshua Bloch in Effective Java

PRIVATE CONSTRUCTORS - They have their own uses in patterns like Singleton, but have their own limitations.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
1

Some of my own findings from Effective Java Item 15, pasting relevant statements from the same

"Recall that to guarantee immutability, a class must not permit itself to be subclassed. Typically this is done by making the class final, but there is another, more flexible way to do it. The alternative to making an immutable class final is to make all of its constructors private or package-private, and to add public static factories in place of the public constructors (Item 1).

While this approach is not commonly used, it is often the best alternative. It is the most flexible because it allows the use of multiple package-private implementation classes. To its clients that reside outside its package, the immutable class is effectively final because it is impossible to extend a class that comes from another package and that lacks a public or protected constructor. Besides allowing the flexibility of multiple implementation classes, this approach makes it possible to tune the performance of the class in subsequent releases by improving the objectcaching capabilities of the static factories.

"

static factory vs constructor advantages are then discussed

HungryForKnowledge
  • 289
  • 1
  • 8
  • 18
  • How does he justify his claim, that "to guarantee immutability, a class must not permit itself to be subclassed"? – Dawood ibn Kareem Nov 30 '12 at 17:57
  • @DavidWallace Right from Effective Java , stating one of the rules to make a class immutable 2. Ensure that the class can’t be extended. This prevents careless or malicious subclasses from compromising the immutable behavior of the class by behaving as if the object’s state has changed. Preventing subclassing is generally accomplished by making the class final, but there is an alternative that we’ll discuss later. Apparently the private constructor, factory method is the alternative – HungryForKnowledge Nov 30 '12 at 17:58
  • But making all fields final and private would also prevent nasty subclasses from doing nasty things. This seems to me like overkill. – Dawood ibn Kareem Nov 30 '12 at 18:03
  • Well..one more pointer in this direction..this alternative would ensure reuse of immutable class instances..much like the Singleton..Immutable objects also being thread-safe – HungryForKnowledge Nov 30 '12 at 18:07
  • 1
    @DavidWallace I believe his point is that subclasses could *add their own* fields, of mutable types, with setter methods etc. – Grundlefleck Dec 03 '12 at 00:49
  • Right. I think it makes more sense to talk about "immutable objects" than "immutable classes". Otherwise it's not clear exactly what is meant. – Dawood ibn Kareem Dec 03 '12 at 02:13
  • @DavidWallace: Making methods `final` would prevent other classes *written by the same author in the same package* from overriding them. The author may prevent the definition of "evil" class derivatives in other packages by making all constructors package-private, and may prevent the definition of "evil" derivatives in the class' own package by simply not writing any. – supercat Apr 27 '14 at 21:34
  • I don't understand how you can implement package private classes if the constructor is private? If you try to extend it even in the same package, it will fail. – Ludvig W Jul 19 '17 at 14:59
0

That is because with private constructor we can not make a subclass of it, and thus restricting all paths of any other class extending it. It is sophisticated because it has its own limitations/complications like in singleton pattern.

vishal_aim
  • 7,636
  • 1
  • 20
  • 23
  • 1
    final class can not be extended. :-(... So you will restrict the class to be extended by just making it final... – Vishal Nov 30 '12 at 17:09
  • yes, that is why making final is the simplest way, and making constructor private is more sophisticated approach. – vishal_aim Nov 30 '12 at 17:12
  • How will u create instance? Using factory method, but how will it assist? Sophistication does not complications in the code... – Vishal Nov 30 '12 at 17:16
  • yes, using factory method. Using factory method doesn't assist but private constructor assist in achieving immutability. there are other things to be done when using private constructor which do not assist. Private constructor is just one of the ways for immutability and not the best one. – vishal_aim Nov 30 '12 at 17:21
  • Okay, how is it different from marking the class final? Can u change the state of that class? I could understand if we are trying to implement something very similar to string, otherwise it is not required to complicate the code – Vishal Nov 30 '12 at 17:29
  • I already agree it is not a preferred/complete approach just to achieve immutability and can complicate the code. – vishal_aim Dec 02 '12 at 03:20
0

The idea behind the private constructor is that you want to hide the immutable implementation of the class data whilst allowing construction of a new instance with different data of the same internal type.

For example

public class Temperature
{
    private readonly double temperatureInCelsius;

    public Temperature(string temperatureInCelsius)
    {
         this.temperatureInCelsius = System.Convert.ToDouble(temperatureInCelsius);
    }
    private Temperature(double temperatureInCelsius)
    {
         this.temperatureInCelsius = temperatureInCelsius;
    }

    public Temperature AddCelsius(string temperatureToAddInCelsius)
    {
         return new Temperature(System.Convert.ToDouble(temperatureToAddInCelsius) + temperatureInCelsius);
    }
    public void PrintCelsius(Display display)
    {
        display.Print(temperatureInCelsius);
    }
    public void PrintFarenheit(Display display)
    // ... etc
}

Ignoring the semi stupidity of the example, if your requirements are that the class can be constructed from a string representing the temperature. The way it actually stores the temperature is subject to change and is an implementation detail. This class can be changed to use floats, strings, doubles, ints etc. This method maintains immutability whilst allowing flexibility on implementation. Obviously this becomes much more powerful when you are wrapping more complex objects such as collections, dictionaries etc.

Michael Parker
  • 7,180
  • 7
  • 30
  • 39
  • 1
    A fundamental advantage of using a package-private constructor is that a class `Foo` with a public constructor promises that code which calls `new Foo(whatever)` will get a new object instance and its exact type will be `Foo`, but many immutable classes may not want to make such a promise. If a class author who expects many requests for the same temperatures might include a static `WeakDictionary`, so repeated requests with the same string would yield the same temperature. Exposing a public constructor would make such a thing impossible. – supercat Jan 09 '14 at 23:34