JavaNovice
JavaNovice

Reputation: 1142

java.lang.String class' value variable

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?

Upvotes: 1

Views: 2532

Answers (3)

Sameer Naik
Sameer Naik

Reputation: 84

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.

Upvotes: 0

venture
venture

Reputation: 79

  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.

Upvotes: 0

Mr. DROP TABLE
Mr. DROP TABLE

Reputation: 332

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

Upvotes: 1

Related Questions