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

to ensure immutability

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i read in one of the ibm pages that in order to ensure immutability and to preserve thread safety, it is important that you not allow the this reference to escape from the constructor.

could some please explain this? thanks!
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Kakkar:
i read in one of the ibm pages that in order to ensure immutability and to preserve thread safety, it is important that you not allow the this reference to escape from the constructor.

could some please explain this? thanks!



It's wrong (ala IBM).
An immutable type is one in which all operations are monadic.
I'm guessing what the article is trying to describe is that an immutable type should be declared final - this too, is wrong. It implies that "immutability" is a subset of the contract of "mutability" - which is a false assumption (in a general sense).

The de facto example is java.lang.String - you can bet your life savings that all method invocations on the type are monadic (reflection hacks, etc. withstanding). If it were not declared final, this does not prevent java.lang.String from being immutable - merely, it's subtype - general inferences which are typically made from subtype to supertype are on a false premise.

All of this is beside the point that "immutability" belongs on the contract - not the implementation - merely, that Java cannot compile-time enforce it. Some other existing (and those in the making) languages, that more closely follow analysis in language theory, can achieve compile-time enforced immutability.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you provide a link to that page? Without more context I find it hard to understand what might be meant, although I suspect that Jeff might be on the right track. I don't see how it could have anything to do with making the class final...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like Brian Goetz' Safe Construction Techniques. It's generally a bad idea to to do anything in a constructor which would make a reference to the current instance available to another thread. For example, registering "this" as an event listener. The problem being that it becomes possible for for the other thread (in this case, the even dispatch thread) to use that reference to access the current object, event though the constructor has not completed yet - which may mean that some of the fields may not be initialized yet. And thanks to the Java memory model, even if you think you've already initialized those fields before registering the listener, other threads may not see it that way. So the event dispatch thread might get a NullPointerException trying to utilize some field was supposed to be initialized during construction.

Rahul, most of the things that I might say here are already in the article. Can you be more specific about what parts you'd like explained?
[ February 28, 2006: Message edited by: Jim Yingst ]
reply
    Bookmark Topic Watch Topic
  • New Topic