File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes final instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "final instance variables" Watch "final instance variables" New topic

final instance variables

agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Why final instance variables of a servlet are considered to be thread safe?

<i>--Agrah Upadhyay--</i><br />Final Year B.Tech SCJP,SCWCD,SCBCD <br /> <br /><b>Now since the real test for any choice is having to make the same choice again,knowing full well what it might cost.</b>-Oracle
Vijay Venkat
Ranch Hand

Joined: Aug 12, 2001
Posts: 52
1. final --> implies it is immutable.
It holds good in any java environment.
Beware: If you are using object, if the properties inside the objects are not final then they are mutable and not thread safe.

Eg private final int _FIVE = 5
private final List _SHAPES = new ArrayList();

Here you cannot assign new values to _FIVE and _SHAPES cannot be made to refer to a different list.

This is invalid : Somewhere in your code you cannot do
_FIVE = 10
_SHAPES = new ArrayList()
On doing above you will get compilation errors.

But the objects with in _SHAPES can be mutated, depending up the objects which have been added to _SHAPES.

Vijay Venkataraman
I agree. Here's the link:
subject: final instance variables
Similar Threads
Final variables
Interface can access
can you figure this out?
By convention - are there for exemple first variables, second getters and setters and third methods?
Spot the difference