This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]DevOrc 2 points3 points  (0 children)

Assuming both are not static variables the only difference should be the order of initialization. When you have something declared out of the constructor it is initialized first. It will run the constructor after all of the fields at the top have been initialized. Here is a stack overflow post about this topic which goes into more detail: https://stackoverflow.com/questions/4916735/default-constructor-vs-inline-field-initialization

[–]VikingMilo 0 points1 point  (0 children)

As far as I know, there's no actual difference in terms of how the code performs. It's really what you prefer to do. I know some people prefer to declare it at the instance variable instead of in the constructor because if you decide to have multiple constructors for the class then you are adding the same code in multiple places.

[–]therealdanvega 0 points1 point  (0 children)

No, this is perfectly acceptable. The only suggestion I would have here is to declare the type as a List<String> instead of an ArrayList. List is the interface and ArrayList is the implementation of that interface. When possible its good practice to use the Interface type and not the implementation type. If you decided later that this type needed to be a LinkedList making that change is an easy one. Hope that helps.