Re: [Eclipse] How to fix indentations.



Lew expounded:
Why do you declare the list static but initialize it in the
constructor? Wouldn't it be better to initialize the list
statically, or else declare it as an instance variable

Pseudo Silk Kimono wrote:
Lew, perhaps I am wrong but is it not the case that my static ArrayList is only created once. I was going to put the creation in the method that picks and displays the random fortune, but I knew that since I only needed it to be created once, why have the JVM create it every time.

it is /created/ once, because it's static, but it is /filled/ with each instantiation of the class, because you put all the add() calls in the constructor instead of a static initializer.

This will only show up if you create more than one instance of the class in the same JVM with the same ClassLoader.

Apropos of that collection, don't mix generics and raw types.

The variable should be of the interface type, not the concrete
implementation.


I don't understand this. I used Netbeans to do all the initial work and added the fortune picking piece in Eclipse once I had the layout working and the button doing something.

The IDE has nothing whatsoever at all to do with what I was saying.

You declared the variable as type ArrayList<String>. It should have been declared as List<String>. You instantiated it as a raw ArrayList. You should have instantiated it as an ArrayList<String>.

See Joshua Bloch's excellent book /Effective Java/ on preferring interfaces to concrete implementations for variable types.

--
Lew
.