I am trying to assign values to String variables within an array:
String details = "passFirst0,passLast0,Molly,Quinn";
Passenger passenger = new Passenger(details);
public class Passenger {
private String firstNameField;
private String lastNameField;
private String firstName;
private String lastName;
public Passenger(String details) {
String[] temp = details.split(",");
String[] fields = {firstNameField, lastNameField, firstName, lastName};
for (int ctr = 0; ctr < fields.length; ctr++) {
fields[ctr] = temp[ctr];
}
// Print instance variables - all null
System.out.println(this.firstNameField);
System.out.println(this.lastNameField);
System.out.println(this.firstName);
System.out.println(this.lastName);
// Print array - has values
System.out.println(Arrays.toString(fields));
}
// Methods
}
However, the instance variables themselves remain null, while the fields[] has values when you iterate through the array.
Why is this so and how to accomplish this?