This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes final ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final ArrayList" Watch "final ArrayList" New topic
Author

final ArrayList

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
hi,

I am really confused with this question:

final List ar =new ArrayList();

ar.add("abc");

it will compile and execute with out giving error.

then why we need to put final here?

In case If I want to create readonly arraylist object what can i do ?

so that by using ar.add("abc"); will given an unmodifiable exception

Please help me out


thnaks
Sai


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
In case If I want to create readonly arraylist object what can i do ?
so that by using ar.add("abc"); will given an unmodifiable exception


java.util.Collections has methods for that kind of task. have a look at the API docs.


SCJP 5.0, SCJD in progress
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
It's your object reference ar that is final. You couldn't assign a different ArrayList to it. This would give you an error


Joanne
 
jQuery in Action, 2nd edition
 
subject: final ArrayList