• 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

Object mutability

 
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Referring to: Head First Servlets & JSP: page 204. Quote: "(And since you're a Java programmer you know that even a final variable can still be manipulated unless it's immutable)"
This topic is not about servlets or jsp.

How would one mutate an object that is final? By referencing the object and then doing a mutate() operation on it? e.g.:
final Object o = new Object();
o.mutate();

I don't understand. If it was declared final, then how come you are allowed to mutate it (or is that not true)?

How would one make the Object immutable then? Meaning: not mutable in any way, not even by reference.
How do I make sure a reference cannot modify the object it refers to? So that MyObject o = new Object(); o.modify(); is not possible?

I also read on Wikipedia the following example:
final Position pos = new Position(); //pos is immutable
members:
pos.x, pos.y, pos.z are mutable still, unless they are declared final.

So if I am understanding this correctly, in order to make an object/class completely immutable, everything in it including its reference has to be declared final? e.g.:

final Position pos = new Position();

class Position {
final int x;
final int y;
final int z;

public Position(){
x=1;
y=2;
z=3;
}

final void doA(){}
final void doB(){}
}

I am confused on this topic.
 
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
Objects aren't final; references variables are.
 
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How would one make the Object immutable then? Meaning: not mutable in any way



Here is a helpful page.
http://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html

And here is a very helpful post.
https://coderanch.com/t/613910/java/java/Creating-Immutable-Classes-object-reference

HIH

Chan.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Darryl already mentioned, objects cannot be final; variables can be.

You have to be aware of the distinction between objects and variables; they're not the same thing. A variable* is a reference (i.e., a pointer) to an object. Making a variable final means that you can't change the variable, in other words, you can't make the variable refer to another object. It doesn't mean that you cannot change the state of the object that the variable refers to.

To have an immutable object, the class that the object was made from has to be designed to be immutable. Some classes in Java's standard library are immutable, and some aren't. For example, class String is immutable; once a String object is initialized, its content cannot be modified (and it doesn't matter if your variable that refers to the String is final or not). Class Date is mutable; you can change its contents (even if the variable that refers to it is final).

To make a class immutable, you'll have do a few things: make all member variables final, do not add any methods to the class that can change the state of the object, and make the class itself final so that subclasses can't make it mutable.

You have to be careful, because subtle things can go wrong. If you have any member variables that are of a mutable type, you should be careful that you don't let them leak out, so that outside code might indirectly change the state of the object. Here's an example with two bugs:

There are two places where this class has leaks; through the constructor, and through the getDate() method:

To fix these, you'll have to make copies of the Date object in the constructor and the getter:


* of a non-primitive type
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fantastic example. Thank you so much, Jesper de Jong.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
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