Reputation: 5472
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?
Upvotes: 0
Views: 9874
Reputation: 5472
As for the "how", I ultimately went with Andreas' first comment:
public Passenger(String details) {
String[] temp = details.split(",");
this.firstNameField = temp[0];
this.lastNameField = temp[1];
this.firstName = temp[2];
this.lastName = temp[3];
}
Realizing the simplest approach ain't so bad.
Upvotes: 0
Reputation: 524
String objects are immutable - you cannot change their value - while doing assignments you are changing object to which the variable (or variable at given array index) is referring to.
When you do:
String[] fields = {firstNameField, lastNameField, firstName, lastName};
you set reference of fields
array value with index
0 to same object that firstNameField
is referring to (in this case null
), index
1 to refer to same object as lastNameField
, etc.
Then, if you do:
fields[ctr] = temp[ctr];
you are not changing value of the object fields[ctr]
was referring earlier (one of your instance variables) to value of temp[ctr], but rather you are setting fields[ctr]
to refer to the same object temp[ctr]
is referring to right now.
If you want to refer to your variables as an array, why won't you just declare them as an array from the beginning?
Upvotes: 3
Reputation: 159260
I would advise against this, but you could use Java 8 method references:
public class Passenger {
private String firstNameField;
private String lastNameField;
private String firstName;
private String lastName;
public Passenger(String details) {
List<Consumer<String>> setters = Arrays.asList(this::setFirstNameField,
this::setLastNameField,
this::setFirstName,
this::setLastName);
String[] temp = details.split(",");
for (int i = 0; i < setters.size(); i++) {
setters.get(i).accept(temp[i]);
}
}
public String getFirstNameField() {
return this.firstNameField;
}
public void setFirstNameField(String firstNameField) {
this.firstNameField = firstNameField;
}
public String getLastNameField() {
return this.lastNameField;
}
public void setLastNameField(String lastNameField) {
this.lastNameField = lastNameField;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Passenger [firstNameField=" + this.firstNameField +
", lastNameField=" + this.lastNameField +
", firstName=" + this.firstName +
", lastName=" + this.lastName + "]";
}
}
Test
String details = "passFirst0,passLast0,Molly,Quinn";
Passenger passenger = new Passenger(details);
System.out.println(passenger);
Output
Passenger [firstNameField=passFirst0, lastNameField=passLast0, firstName=Molly, lastName=Quinn]
Upvotes: 3
Reputation: 706
in "fields" you have the values of the split of "details" that you set in the for loop and in the instance variables you have nothing (They weren't initialized with data)
Upvotes: 0
Reputation:
They are null because they haven't been set, you can set them like this;
public class Passenger {
private String firstNameField = "a";
private String lastNameField = "b";
private String firstName = "c";
private String lastName "d";
Or with arguments to the constructor
like this;
public Passenger(String details, String f, String l, String fn, String ln) {
firstNameField = a;
lastNameField = l;
firstName = fn;
lastName = ln;
.....
}
As stated in the comments, you do overwrite the array values within the for loop, but you try to print out class members;
System.out.println(this.firstNameField);
System.out.println(this.lastNameField);
System.out.println(this.firstName);
System.out.println(this.lastName);
which are uninitialised.
Why not set the array like below;
String[] temp = details.split(",");
String[] fields = {temp[0], temp[1], temp[2], temp[3]};
And why bother with the other variables, just create getters
for each needed variable, eg;
public String getFirstName() {
return fields[2];
}
Upvotes: 1