i working on assignment , i'm stuck on error: cannot assign value final variable count
here code far...
public class list { private final int max = 25; private final int count; private person list[]; public list() { count = 0; list = new person[max]; } public void addsomeone(person p) { if (count < max){ count++; // error occurs list[count-1] = p; } } public string tostring() { string report = ""; (int x=0; x < count; x++) report += list[x].tostring() + "\n"; return report; } }
i'm new java , not computer whiz please explain problem/solution in simplest terms possible. thank much.
count++;
throw error. per oracle,
a final variable may assigned once. declaring variable final can serve useful documentation value not change , can avoid programming errors.
you can follow along article here. looking @ code, seems don't want count
final. want able change value throughout program. fix remove final
modifier.
Comments
Post a Comment