I've been going through the String.class file (java.lang.String) and I have a couple of questions.
The class has a char[] declared as final and the variable name is value. There is a constructor like below through which the value of the char[] is set.
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
My questions are:
1) How do they set the value of a final variable through a constructor?
2) Secondly, the equals method
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
In this method, if anObjectis an instance of String, the method assigns anotherString.value to a char array. However, when I try to do String.value, I get an error "value is not visible". I assume because it's declared as private in String class, but how is the String class able to use anotherString.value on a String instance?