• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Final Variables immutable class

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not necessary. If your class holds final references to mutable objects, it is effectively a mutable class.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80636
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80636
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic