How can this implementation of string constructor (java) work?

 public String(String original) {
     int size = original.count;
     char[] originalValue = original.value;
     char[] v;
     if (originalValue.length > size) {
         // The array representing the String is bigger than the new
         // String itself.  Perhaps this constructor is being called
         // in order to trim the baggage, so make a copy of the array.
         int off = original.offset;
         v = Arrays.copyOfRange(originalValue, off, off+size);
     } else {
         // The array representing the String is the same
         // size as the String, so no point in making a copy.
         v = originalValue;
     }
     this.offset = 0;
     this.count = size;
    this.value = v;
 }

I apologize if I am stupid, but I do not understand this. the count and value fields are private, but this code somehow directly reaches these values. How can it be?

+3
source share
1 answer

private means "accessible only to the class" and not "accessible only to the object".

+5

All Articles