File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final ArrayList

 
saikrishna cinux
Ranch Hand
Posts: 689
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas Thevis
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's your object reference ar that is final. You couldn't assign a different ArrayList to it. This would give you an error
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic