This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How can i make a class Immutable if it has an instance variable of java.lang.Object?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final public class ImmutableWithObject {

final Object obj;
final List myList;

ImmutableWithObject(Object obj1, List list) {
this.obj = obj1;
this.myList = ((List) ((ArrayList) list).clone());
}

public Object getObj() {
return this.obj;
}

public List getMyList() {
return (List) ((ArrayList<String>) this.myList).clone();
}

public static void main(String[] args) {

ImmutableWithObject io = new ImmutableWithObject(new Date(),
new ArrayList());

((Date) io.getObj()).setDate(22);

System.out.println((Date) io.getObj());
}
}

Result : Mon Aug 22 00:50:04 IST 2011

wich is incorrect.
 
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't do this:

By allowing other classes access to the object, you destroy the immutability of the class.
 
Amit Cngh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then Paul , how can we get the copy of Object?
 
Paul Clapham
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha. So your question is really the old FAQ about how to clone an arbitrary Object. To which the answer is that you can't.
 
Amit Cngh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So , we can't make ImmutableWithObject class immutable ?
 
Marshal
Posts: 67289
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't. Any class that exposes non-immutable members cannot be immutable.
 
Amit Cngh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that i have done with List . Instead of using reference to list we can use can use deep copy.and it will become Immutable.

 
A berm makes a great wind break. And Iwe all like to break wind once in a while. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!