Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, let's discuss what a "forward reference" is and why it is bad. A forward reference is a reference to a variable that has not yet been initialized, and it is not confined only to static initalizers. These are bad simply because, if allowed, they'd give us unexpected results. Take a look at this bit of code:</p> <pre><code>public class ForwardRef { int a = b; // &lt;--- Illegal forward reference int b = 10; } </code></pre> <p>What should j be when this class is initialized? When a class is initialized, initializations are executed in order the first to the last encountered. Therefore, you'd expect the line </p> <pre><code>a = b; </code></pre> <p>to execute prior to: </p> <pre><code>b = 10; </code></pre> <p>In order to avoid this kind of problems, Java designers completely disallowed such uses of forward references. </p> <p><strong>EDIT</strong></p> <p>this behaviour is specified by <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html" rel="nofollow noreferrer">section 8.3.2.3 of Java Language Specifications</a>:</p> <blockquote> <p>The declaration of a member needs to appear before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:</p> <ul> <li><p>The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.</p></li> <li><p>The usage is not on the left hand side of an assignment.</p></li> <li><p>C is the innermost class or interface enclosing the usage.</p></li> </ul> <p>A compile-time error occurs if any of the three requirements above are not met.</p> </blockquote>
    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. 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