• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Declaration of List as a class final variable

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Master Rancher
Posts: 4280
57
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha. Overlooked that one.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if the contents is a bit more dynamic (or large) you can use a static initializer:
 
Ranch Hand
Posts: 30
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Kacem Bel
 
Mike Simmons
Master Rancher
Posts: 4280
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch Kacem Bel



Thank you ^^
 
Kacem Bel
Ranch Hand
Posts: 30
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thanks for the help guys!
 
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic