• Post Reply Bookmark Topic Watch Topic
  • New Topic

Declaration of List as a class final variable  RSS feed

 
Peter Fat
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I want to have a List<String> with 6 elements that will always be the same.

I currently have



But how do I define the elements right at the moment when I declare the variable? Thanks!
 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Fat wrote:Hi!

But how do I define the elements right at the moment when I declare the variable? Thanks!


But why would you want to do that? You can always add the element later to the list (Declaring it as final means you cannot assign it to another List but you can always add content to it).
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, often it's nice to know that an object is immutable. Particularly if there are any threading issues - you don't have to worry about the thread safety of an immutable object. Ever. So I think it's perfectly reasonable to want to create an immutable list of objects, and to want to do that as concisely as possible.
It's tempting to do

but this isn't really immutable. Better to do something like this:

or, using Google collections:

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:... It's tempting to do

but this isn't really immutable. ...

Why isn't that immutable? You can't add or remove anything.
 
Peter Fat
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:
Peter Fat wrote:Hi!

But how do I define the elements right at the moment when I declare the variable? Thanks!


But why would you want to do that? You can always add the element later to the list (Declaring it as final means you cannot assign it to another List but you can always add content to it).


This list will be used to create the keys for an HashMap, and these will always be the same. But since I'm beginning programming, I should ask: how is this done in the more professional way?
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you can replace elements using set. (Reply to Wouter's question.)
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha. Overlooked that one.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Fat wrote:This list will be used to create the keys for an HashMap, and these will always be the same. But since I'm beginning programming, I should ask: how is this done in the more professional way?

Java does not have a literal syntax for collections such as lists, maps etc. (this might be a feature that will be included in some future version of Java). Mike's answer is good if you really want an immutable list. I'd also make the member variable static; you do not need an instance of the list for each object that you create with the class. The usual convention for static final variables in Java is to have all-capital names:
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if the contents is a bit more dynamic (or large) you can use a static initializer:
 
Kacem Bel
Ranch Hand
Posts: 30
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:
Mike Simmons wrote:... It's tempting to do

but this isn't really immutable. ...

Why isn't that immutable? You can't add or remove anything.



Yes you are right it is immutable...
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Kacem Bel
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kacem Bel wrote:
Wouter Oet wrote:
Mike Simmons wrote:... It's tempting to do

but this isn't really immutable. ...

Why isn't that immutable? You can't add or remove anything.

Yes you are right it is immutable...

No, it isn't. You are overlooking the subsequent replies from Rob and then Wouter:
Rob Spoor wrote:But you can replace elements using set. (Reply to Wouter's question.)

Wouter Oet wrote:Aha. Overlooked that one.

Because you can call set() on the list returned by Arrays.asList(), it is not immutable.
 
Kacem Bel
Ranch Hand
Posts: 30
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch Kacem Bel


Thank you ^^
 
Kacem Bel
Ranch Hand
Posts: 30
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
Kacem Bel wrote:
Wouter Oet wrote:
Mike Simmons wrote:... It's tempting to do

but this isn't really immutable. ...

Why isn't that immutable? You can't add or remove anything.

Yes you are right it is immutable...

No, it isn't. You are overlooking the subsequent replies from Rob and then Wouter:
Rob Spoor wrote:But you can replace elements using set. (Reply to Wouter's question.)

Wouter Oet wrote:Aha. Overlooked that one.

Because you can call set() on the list returned by Arrays.asList(), it is not immutable.


ah sorry infact i will pay more attention next time :P
 
Peter Fat
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for the help guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!