• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final instance variables and thread safety

 
Reema Patel
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I believe instance variables marked with the 'final' modifier are thread safe.

eg
private final String jndiPath = "env/something"; should be thread safe?

Please correct me if I'm wrong. I had come across a mock exam which contradicted from my view.

Thanks
Reema
 
joko mujoko
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...

I agree with yu, I do some mock eaxm, and it old me that final property of instance class is thread safe.

Joko
 
Adam Czysciak
Ranch Hand
Posts: 90
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Reema Patel:
I believe instance variables marked with the 'final' modifier are thread safe.

Yes, but only for immutable objects (String, Integer etc...) or primitives. Eg if you have final ArrayList, then it's obviously not thread safe.
 
Reema Patel
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Adam!
This sounds very apt. final modifier means that the reference can't be made pointing to some other on the heap, but, there's nothing preventing a servlet to modift its contents.

Thanks
Reema
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic