2013年9月17日星期二

Modified by final member variables must be assigned an initial value of it ?

I define a member variable final int type , there is no assigned an initial value , eclipse directly prompt an error , dos compiler also through , but a lot of the String class inside but did not give the final wording of the definition of variable assignment , which is why it ?

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence
{
    /** The value is used for character storage. */
    private final char value[];

    /** The offset is the first index of the storage that is used. */
    private final int offset;

    /** The count is the number of characters in the String. */
    private final int count;

String class is final modified offset variable looks like in the source code is assigned several times , this is why ? Or I do not understand what ?
------ Solution ---------------------------------------- ----
first said final keyword , grooming different content meaning:
final class can not be inherited , there is no sub-class , final class method default is final.
final method can not cover quilt class method , but it can be inherited.
final member variables represent constants, can only be assigned once, after the assignment value is no longer changed.
final can not be used to modify the constructor.

you doubt is the third

java 's String class member variable offset with final modification . But did not carry out the assignment .
you also can be understood as offset is a blank ID card, ID card each constructor method is for printing operation.

String class has multiple constructors , each method are carried out assignments for the offset process , once the assignment no longer be modified once .
we create an object , only call the class constructor to instantiate a , so there would not be final offset is assigned the case many times .
For example:
String str = new String (byte bytes [], int offset, int length);
String str1 = new String (byte bytes [], int offset, int length, Charset charset);

Also important to note is that the picture is circled two offset is not the same .
a method parameter is the offset, one is a class member variable offset ( what you say final offset)


------ For reference only ---------------------------------- -----
in the constructor copy
------ For reference only ------------------------- --------------
final members can be assigned in the constructor Well
------ For reference only ----------- ----------------------------
 on the top floor . .
------ For reference only -------------------------------------- -
final member variables represent constants, can only be assigned once, after the assignment value is no longer changed.
------ For reference only -------------------------------------- -
do not need to pay the value when declared , but only pay the value of a
------ For reference only --------------------- ------------------
to add points , final modification of the index , the object can not be changed , but the contents of the object can be changed.
For example:
final A a = new A ();
a.value = 2;
this is possible, but if you then being given a new assignment : a = new A ();
------ For reference only -------- -------------------------------

+1, top.
------ For reference only -------------------------------------- -
String final definition of the class object constructor in his copy .
public String() {
this.offset = 0;
this.count = 0;
this.value = new char[0];
    }

    /**
     * Initializes a newly created {@code String} object so that it represents
     * the same sequence of characters as the argument; in other words, the
     * newly created string is a copy of the argument string. Unless an
     * explicit copy of {@code original} is needed, use of this constructor is
     * unnecessary since Strings are immutable.
     *
     * @param  original
     *         A {@code String}
     */
    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;
    }


his design philosophy is to affirm , without initial value , which is called final variable blank, but before using the blank final must be initialized in the constructor of the assignment .
------ For reference only -------------------------------------- -

---- - For reference only ---------------------------------------
right upstairs almost the same. .
------ For reference only -------------------------------------- -
either the assignment statement , either in the constructor in the assignment
------ For reference only ------------------------ ---------------
Thank you for your answer , the original is in the constructor assigned. I tested the next , you can also assign a code block .
But there is a problem brother , I see a lot of the String class constructor overloads , so I wrote a overloaded constructor methods to give final variable assignment

public class Test25_final {
private final int x;

public Test25_final(){
x = 5;
}

public Test25_final(int xx){
this.x = xx;
}

public static void main(String[] args){
Test25_final tf = new Test25_final();
System.out.println(tf.x);

Test25_final tf2 = new Test25_final(6);
System.out.println(tf2.x);

Test25_final tf3 = new Test25_final(7);
System.out.println(tf3.x);
}
}

When I print print 5,6,7 respectively . Why is this the first time I call the constructor method has been given the assignment of x , followed by the construction method is still effective, and re- assignment of a given x . . This is why , or where it is my understanding wrong ? this x plus without are not affected , and that final in this class in the end it worked ?
------ For reference only -------------------------------------- -


Here you have created three Test25_final objects , each object with a re-initialization , which is to re- call the constructor method, so , so your final value is not the same, because they are irrelevant to each other .
use my example above is that you now have three blank ID card, by instantiating copy constructor to go for identity cards .
you are new times have you decided a few cards.
I do not know if you can understand it.

drew a map , refer to the bar .

------ For reference only ----- ----------------------------------
you write here dead , private final int x = 5 ;
try should not fixating .
Test25_final tf = new Test25_final ();
System.out.println (tf.x);

Test25_final tf2 = new Test25_final (6);
System.out.println (tf2.x);

Test25_final tf3 = new Test25_final (7);
System.out.println (tf3.x);
Here you every time a new object , each new object is going to be a re- call the constructor , re- assignment
------ For reference only --------- ------------------------------
something like this , thank you very much , I basically understand.

没有评论:

发表评论