sunrisepoet sunrisepoet - 22 days ago 7
Java Question

ArrayList throwing syntax error "Unknown class"

Doing some tutorials on java this simple foreach loop on a list is giving me an error in the foreach loop: "Unknown class: familyMembers". I dont see where the problem is. but i know is something simple. Thanks in advance.

Update: I have all the imports necessary.

List<String> familyMembers = new ArrayList<String>();

familyMembers.add("john");
familyMembers.add("suzy");
familyMembers.add("smith");

for (String name : familyMembers){
System.out.println(name);
}

Answer

it looks like familyMembers is a method-locally declared variable

the for loop must be implemented in a method where the familyMembers can be access or scope

Comments