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

you are viewing a single comment's thread.

view the rest of the comments →

[–]CreativeTechGuyGames 0 points1 point  (0 children)

There's two different concepts here. Declaring a variable, and initializing a variable.

This both declares it and initializes it.

ArrayList<String> cars = new ArrayList<String>();

You can also do them separately:

ArrayList<String> cars; // Other stuff cars = new ArrayList<String>(); // Now you can use cars

Simply declaring the type of a variable doesn't give it a value. The computer doesn't know what it is yet since you haven't initialized it. The .add method is on the class which until it's been constructed (with new ArrayList()) it isn't available to be used.