1

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?

5
  • Just as an aside, I think that actual platform code is probably not a great place to start if you are just starting to learn. I'd look at tutorial and simple example code first. Commented Jun 10, 2016 at 15:44
  • 1
    Within a class, private methods and fields can be accessed in other objects Commented Jun 10, 2016 at 15:49
  • 1
    1) Constructors are allowed to set final instance variables on the instance they are constructing. It's one of the places you generally expect to see final variables being set. Commented Jun 10, 2016 at 15:50
  • 1
    You sent variables in constructors; final means the reference never changes after that. Since String is immutable, the value the reference points to can never change either. A class always has access to its own private parts, but you don't. Commented Jun 10, 2016 at 15:50
  • Got it.. Thanks all! You all are awesome! Commented Jun 10, 2016 at 16:04

3 Answers 3

1

1.) Setting the value of a final variable is something you're allowed to do in a constructor because the final variable is new every time the constructor is called. In other words, each time you call the constructor, you make a new object, which has a new final variable that you can set.

2.) Like @ControlAltDel said "Within a class, private methods and fields can be accessed in other objects", but if you're looking to just set the string to an array of characters, just use "anotherString.toCharArray()"

Hope this helps! Have fun with Java

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

Comments

0
  1. If a variable is declared as static final then it will be statically set for that class and all instances of that class. If its declared final non statically, then it must be initialized and set by each instance, but cannot be further modified after being initialized.

  2. The value member is not visible because it's probably declared private. Additionally, it's final so it's not modifiable. This is an important concept because Java strings are immutable.

Comments

0

1) Final variables need not be initialized at its declaration, but it must be initialized (assigned a value) once before it is used. It is possible to assign final instance variable a value only once, in any one of the ways viz. a) During declaration itself. b) Instance initializer block. c) Constructor.

If variable is assigned value during declaration, Java compiler does not allow reassignment either in instance initializer or Constructor.

If instance variable is not assigned value during declaration then Java compiler would allow value to be initialized in initializer block. In this case initialization in Constructor would not be allowed.

If value is not initialized during declaration nor in the instance initializer block, then value must be initialized in Constructor, else there would be compile time error saying Blank final field may not have been initialized.

2) Within a class it is possible to access the private member variables of the other object, if the other object is instance of the same class.

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.