• Post Reply Bookmark Topic Watch Topic
  • New Topic

Final Variables immutable class  RSS feed

 
karimkhan pathan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can i know if all the fields in the class are final ,then that class will become immutable ...Is the statement correct or not ..

please explain ..

regards,
Karim.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not necessary. If your class holds final references to mutable objects, it is effectively a mutable class.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karimkhan:

The final keyword only means that the reference can't be changed. It doesn't mean that the object the reference is referring to can't change internally. As Deepak said, you need to refer to immutable objects, or use some other technique like defensive copying.

John.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karimkhan pathan wrote:Can i know if all the fields in the class are final ,then that class will become immutable ...Is the statement correct or not ..


It's correct if all variables are either primitives or references to immutable objects.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uj nossnahoj wrote: . . .It's correct if all variables are either primitives or references to immutable objects.
. . . and the class is tagged "final" too.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John de Michele wrote:Karimkhan:

The final keyword only means that the reference can't be changed. It doesn't mean that the object the reference is referring to can't change internally. As Deepak said, you need to refer to immutable objects, or use some other technique like defensive copying.

John.


Thanks all of you guys for sharing you'r knowledges ,

What is actually defensive copying ?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s begri wrote:
John de Michele wrote:Karimkhan:

The final keyword only means that the reference can't be changed. It doesn't mean that the object the reference is referring to can't change internally. As Deepak said, you need to refer to immutable objects, or use some other technique like defensive copying.

John.


Thanks all of you guys for sharing you'r knowledges ,

What is actually defensive copying ?


It is a method by which you return a copy of your member variable instead of a reference to it. Google it for more info.

You can also use techniques like Collections.unmodifiableList(). The javadoc should be able to help you understand the technique
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reference to the Foo object is not the same as the original object; you can play with the returned object to your heart's content without changing the state of the original Foo object. Note there are no methods which call the setNumber and setWord methods of the Foo reference. Copying the String in the Foo copy constructor does not require defensive copies because Strings are immutable.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s begri wrote:What is actually defensive copying ?


It's a way of maintaining immutability of a class.

Instead of returning a reference to an internal and mutable data structure, you return a copy. The user can then modify it to his hearts desire without compromising the internals of the object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!