This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 3487
    
  13
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final ArrayList