• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

An immutable class

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

I have a silly yet simple question (studying for the Java SE17 exam). Why does Toad qualify as an immutable class? I thought at a minimum it had to be final? is it the fact that its constructor is private, thus you can't instantiate it?

 
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Bant wrote:

The most common example of immutable is the String class. Toad has an instance variable of String called 'name'. Toad is immutable because 'name' cannot be modified in either of two ways: a) modifying the internal state of the String object referred to by 'name' (as mentioned, it is already immutable), and b) not allowing the reference of 'name' to be modified to refer to anything other than what it was initialized to in the constructor because it was declared 'final'. The fact  that the constructor is private doesn't matter. The fact that Toad is not final means that you can subclass Toad but the subclass may or may not itself be immutable.
 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the most important aspects of making an immutable class are the ones Carey just gave.  But depending who you ask, there is a third aspect that is usually also required: we must ensure that the class cannot be subclasses to make it mutable, either.  Technically, Toad fulfills that requirement by having a private constructor, making it impossible to extend.  As it happens, this also makes the class uninstantiable and pretty much useless, since no other method is provided to allow instantiation.  Normally it would be more straightforward to simply mark the class final, and still allow it to be instantiated.  A private constructor is fine, provided there is also some way to instantiate the class.

Note that, if Toad is a nested or inner class (where the outer class is not shown), then it's possible the outer class contains code that makes it instantiable and/or mutable.  But assuming there's no outer class, then it is uninstantiable and immutable, and generally useless.
 
I can't take it! You are too smart for me! Here is the tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic