• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can classes be immutable in some situations but not others?  RSS feed

 
Mark McKay
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some complex objects I want to design. Most of the time I would like these objects to be immutable so that other classes cannot change their values. However, I also want to create an editor to help create these objects and the editor will need to change the object's values. It would also be useful to be able to set the objects' fields one at a time during serialization rather than doing everything in a huge constructor.

C provides a way to make objects unchangeable by using the 'const' keyword, but Java doesn't have that. I could also wrap my objects in other accessor objects, but then I'm duplicating a lot of code. Are there any good ways to make my objects mutable only to certain other classes?
 
Paul Clapham
Sheriff
Posts: 22836
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. Put the class in a package, and provide it with "package-private" methods (i.e. with no access modifier). Then other classes in the same package (e.g. your editor) can call those methods, but classes in other packages can't.
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The classes in other packages will not have any access to the 'default' methods & variables. I think he wants to be able to access them (but in an immutable state) and the editor in mutable state. Mutability refers to the 'changeability' of an object. What you want is not 'immutability', but you want 'visibility as constants'. The equivalent of C language const keyword in java is 'static final'. Think in terms of accessibility, because that is what you want. The editor would have access to the 'writing' methods and all others would not have access to 'write methods' (only 'read methods').
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make the class variable as final blank and initialize their value through constructor. This make your class immutable as string class is.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote: . . . The equivalent of C language const keyword in java is 'static final'. . . .
No it isn't. The equivalent of const is const. But you can't use const in Java.
You can use const to make a pointer constant of to make its contents unchangeable. There is no keyword you can use in Java to make the state of an object unchangeable. You have to design the object carefully and final is only a part of that.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote: . . . What you want is not 'immutability', but you want 'visibility as constants'. . . .
Agree that is not immutability. Immutable in some situations but not others looks like a contradiction in terms to me.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!