Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>"The verifier rejects code that <strong>uses</strong> the new object before it has been initialized."<br></p> </blockquote> <p>In bytecode verification, since the verifier works at link-time, the types of local variables of methods are inferred. The types of method arguments are known as they are in the method signature in the class file. The types of other local variables are not known and are inferred, so I assume the <strong>"uses"</strong> in the above statement relates to this.</p> <p>EDIT: The section 4.9.4 of the <a href="http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#9839" rel="nofollow noreferrer">JVMS</a> reads:</p> <blockquote> <p>The instance initialization method (<a href="http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html#12174" rel="nofollow noreferrer">§3.9</a>) for class myClass sees the new uninitialized object as its this argument in local variable 0. Before that method invokes another instance initialization method of myClass or its direct superclass on this, the only operation the method can perform on this is assigning fields declared within myClass.</p> </blockquote> <p>This assignment of fields in the above statement is the "initial" initialization of the instance variables to default initial values (like int is 0, float is 0.0f etc.) when the memory for the object is allocated. There is one more "proper" initialization of instance variables when the virtual machine invokes the instance initialization method(constructor) on the object.<br> <br> <br> The <a href="http://scala-programming-language.1934581.n4.nabble.com/Object-initialization-order-in-Scala-td1987968.html" rel="nofollow noreferrer"><strong>link</strong></a> provided by John Horstmann helped clarify things. So these statements dont hold true. "This DOESNOT mean that in an <code>&lt;init&gt;</code> method, <code>getfield</code> and <code>putfield</code> are allowed before another <code>&lt;init&gt;</code> is called." The <code>getfield</code> and <code>putfield</code> instructions are used to access (and change) the instance variables(fields) of a class (or instance of a class). And this can happen only when the instance variables(fields) are initialized."</p> <p>From the JVMS :</p> <blockquote> <p>Each instance initialization method (§3.9), except for the instance initialization method derived from the constructor of class Object, must call either another instance initialization method of this or an instance initialization method of its direct superclass super before its instance members are accessed. However, instance fields of this that are declared in the current class may be assigned before calling any instance initialization method. <br></p> </blockquote> <p><strong>When the Java Virtual Machine creates a new instance of a class, either implicitly or explicitly, it first allocates memory on the heap to hold the object's instance variables. Memory is allocated for all variables declared in the object's class and in all its superclasses, including instance variables that are hidden. As soon as the virtual machine has set aside the heap memory for a new object, it immediately initializes the instance variables to default initial values. Once the virtual machine has allocated memory for the new object and initialized the instance variables to default values, it is ready to give the instance variables their proper initial values. The Java Virtual Machine uses two techniques to do this, depending upon whether the object is being created because of a clone() invocation. If the object is being created because of a clone(), the virtual machine copies the values of the instance variables of the object being cloned into the new object. Otherwise, the virtual machine invokes an instance initialization method on the object. The instance initialization method initializes the object's instance variables to their proper initial values.</strong> And only after this can you use <code>getfield</code> and <code>putfield</code>. <br> <br> The java compiler generates atleast one instance initialization method(constructor) for every class it compiles. If the class declares no constructors explicitly, the compiler generated a default no-arg constructor that just invokes the superclass no-arg constructor. And rightly so doing any operation on an instance field before a call to <code>super()</code> or <code>this()</code> results in a compilation error. <br> <br> An <code>&lt;init&gt;</code> method can contain three kinds of code: an invocation of another <code>&lt;init&gt;</code> method, code that implements any instance variable initializers, and code for the body of the constructor. If a constructor begins with an explicit invocation of another constructor in the same class (a <code>this()</code> invocation) its corresponding <code>&lt;init&gt;</code> method will be composed of two parts:</p> <ul> <li>an invocation of the same-class <code>&lt;init&gt;</code> method</li> <li>the bytecodes that implement the body of the corresponding constructor</li> </ul> <p>If a constructor does not begin with a <code>this()</code> invocation and the class is not Object, the <code>&lt;init&gt;</code> method will have three components:</p> <ul> <li>an invocation of a superclass <code>&lt;init&gt;</code> method</li> <li>the bytecodes for any instance variable initializers</li> <li>the bytecodes that implement the body of the corresponding constructor</li> </ul> <p><br> If a constructor does not begin with a <code>this()</code> invocation and the class is Object(and Object has no superclass), then its <code>&lt;init&gt;</code> method cant begin with a superclass <code>&lt;init&gt;</code> method invocation. If a constructor begins with an explicit invocation of a superclass constructor ( a <code>super()</code> invocation), its <code>&lt;init&gt;</code> method will invoke the corresponding superclass <code>&lt;init&gt;</code>method.</p> <p><br> <br> I think this answers your first and second question.</p> <p><strong>Updated:</strong><br></p> <p>For example, </p> <pre><code> class Demo { int somint; Demo() //first constructor { this(5); //some other stuff.. } Demo(int i) //second constructor { this.somint = i; //some other stuff...... } Demo(int i, int j) //third constructor { super(); //other stuffff...... } } </code></pre> <p>Heres the bytecode for the above three constructors from the compiler(javac): </p> <pre><code>Demo(); Code: Stack=2, Locals=1, Args_size=1 0: aload_0 1: iconst_5 2: invokespecial #1; //Method "&lt;init&gt;":(I)V 5: return Demo(int); Code: Stack=2, Locals=2, Args_size=2 0: aload_0 1: invokespecial #2; //Method java/lang/Object."&lt;init&gt;":()V 4: aload_0 5: iload_1 6: putfield #3; //Field somint:I 9: return Demo(int, int); Code: Stack=1, Locals=3, Args_size=3 0: aload_0 1: invokespecial #2; //Method java/lang/Object."&lt;init&gt;":()V 4: return </code></pre> <p>In the first constructor, the <code>&lt;init&gt;</code> method begins with calling the same-class <code>&lt;init&gt;</code> method and then executed the body of the corresponding constructor. Because the constructor begins with a <code>this()</code>, its corresponding <code>&lt;init&gt;</code> method doesnot contain bytecode for initializing the instance variables.</p> <p>In the second constructor, the <code>&lt;init&gt;</code> method for the constructor has </p> <ul> <li>super class <code>&lt;init&gt;</code> method, ie, invocation of the superclass constructor(no arg method), the compiler generated this by default because no explicit <code>super()</code> was found as the first statement.</li> <li>the bytecode for initializing the instance variable <code>someint</code>.</li> <li>bytecode for rest of the stuff in the constructor body. <br></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload