• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

immutable

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to create immutable class without using final keyword?
please give me a sample code for this?
 
Ranch Hand
Posts: 2102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is immutable.



If you make a virus with type = 1, it can never mutate to any other BirdFluVirus type, e.g. type = 2.

Below is a mutable one.




[Edited: Added 'private' to correct code, as per Matthew's post.]
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello cherry hal,

Welcome to CodeRanch!

cherry hal wrote:How to create immutable class without using final keyword?


Why do you want to do that? I mean to ask - is it part of some assignment, or some project requirement (so that we can see if this can be avoided etc.)

cherry hal wrote:please give me a sample code for this?


Well, this is NotACodeMill. There are ways to create immutable class, but we would appreciate to see your code and hear about what problems you are facing about it.

I hope this helps.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesus Angeles wrote:This is immutable.


Actually, it isn't, because it's got one important flaw. The type variable needs to be private, otherwise anyone can change the value. Other than that, though, you're right. The key part of making a class immutable is simply to not provide any way of changing the state.
 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have always found this as an excellent article on the topic of java immutability
http://www.javaranch.com/journal/2003/04/immutable.htm
 
Bartender
Posts: 10759
68
Hibernate Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

cherry hal wrote:How to create immutable class without using final keyword?


With care. In addition to what Matthew said about Anayonkar's solution, there's also the fact that it can be subclassed.
This means that anyone can create a mutable variant which, even if it can't actually change any of the superclass's state, could still render any claims of Thread-safety invalid.

Another possibility is:The advantage of this pattern is that you can restrict subtyping.

For example, if the constructor was package-private, then the class could only be subclassed by another class in the same package, which is probably a lot safer than letting anyone do it.

Winston (with kudos to Maneesh)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another (perhaps simpler) way to make it impossible to subclass a class is by making the class itself final:

Then you wouldn't have to make the constructor private and the and the getter method final (they can't be overridden since the class can't be subclassed).

Note that in Winston's example it also wasn't really necessary to make the getter method final, because the constructor is private, which means you can't subclass it and override the method.
 
Winston Gutkowski
Bartender
Posts: 10759
68
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Note that in Winston's example it also wasn't really necessary to make the getter method final, because the constructor is private, which means you can't subclass it and override the method.


Ah, maybe you didn't read my postscript; and for that it must be.
Also, cheery's question was specifically about whether it's possible to make a class immutable without using final.

Now, whether you''d want to is another matter...

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!