3
 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 am sorry if I am being stupid, but I don't get it. count and value fields are private, yet this code appears to somehow reach those values directly. How can this be?

1 Answer 1

5

private means "only accessible by the class", not "only accessible by the object".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.