explain this pls

Discussion in 'Java' started by lurer@gmx.net, May 30, 2007.

  1. Guest

    class Out
    {
    int x;
    {
    k = 5; //valid
    x = k; //invalid
    }
    public int k;
    }
     
    , May 30, 2007
    #1
    1. Advertising

  2. Out.java:6: illegal forward reference
    x = k; //invalid
    ^
    1 error
     
    Jussi Piitulainen, May 30, 2007
    #2
    1. Advertising

  3. Guest

    On May 30, 2:27 pm, Jussi Piitulainen <>
    wrote:
    > Out.java:6: illegal forward reference
    > x = k; //invalid
    > ^
    > 1 error


    so "forward assignment" is ok?

    k = 5; //"forward assignment"
    x = k; //compiler error because of forward reference

    ?
     
    , May 30, 2007
    #3
  4. writes:
    > Jussi Piitulainen wrote:
    >> Out.java:6: illegal forward reference
    >> x = k; //invalid
    >> ^
    >> 1 error

    >
    > so "forward assignment" is ok?
    >
    > k = 5; //"forward assignment"
    > x = k; //compiler error because of forward reference
    >
    > ?


    Seems so. A simplest web search suggests that the JLS, possibly in
    section 8.3.2, requires declaration before using a variable in an
    initializer "not on the left hand side of an assignment".
     
    Jussi Piitulainen, May 30, 2007
    #4
  5. Jussi Piitulainen wrote:
    > writes:
    >> Jussi Piitulainen wrote:
    >>> Out.java:6: illegal forward reference
    >>> x = k; //invalid
    >>> ^
    >>> 1 error

    >> so "forward assignment" is ok?
    >>
    >> k = 5; //"forward assignment"
    >> x = k; //compiler error because of forward reference
    >>
    >> ?

    >
    > Seems so. A simplest web search suggests that the JLS, possibly in
    > section 8.3.2, requires declaration before using a variable in an
    > initializer "not on the left hand side of an assignment".


    I suppose that makes sense.

    k = 5; is correct because the assignment of k works, but if x=k; were
    allowed, this could happen:

    int x;
    {
    x = k;
    }
    int k = x;

    What is x and k? k is defined initially in terms of x, which is
    initialized to k, so no definite assignment can be given them.

    In short, forward assignment is harmless, so it is allowed; forward
    reference opens up a can of worms, so it is forbidden.
     
    Joshua Cranmer, May 30, 2007
    #5
  6. Lew Guest

    Jussi Piitulainen wrote:
    > writes:
    >> Jussi Piitulainen wrote:
    >>> Out.java:6: illegal forward reference
    >>> x = k; //invalid
    >>> ^
    >>> 1 error

    >> so "forward assignment" is ok?
    >>
    >> k = 5; //"forward assignment"
    >> x = k; //compiler error because of forward reference
    >>
    >> ?

    >
    > Seems so. A simplest web search suggests that the JLS, possibly in
    > section 8.3.2, requires declaration before using a variable in an
    > initializer "not on the left hand side of an assignment".


    Skipping the web search and going straight to
    <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2>
    we find
    > Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to class variables.


    Going in turn to
    <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#287406>
    we find:
    > The declaration of a member needs to appear textually 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:
    >
    > * The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
    > * The usage is not on the left hand side of an assignment.
    > * The usage is via a simple name.
    > * C is the innermost class or interface enclosing the usage.
    > A compile-time error occurs if any of the four requirements above are not met.


    That last phrase I think is a typo; I interpret it to mean "A compile-time
    error occurs if all of the four requirements above are met and the variable is
    referred to before declaration."

    That the block is an instance initializer is clear from
    <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.6>
    which repeats
    > Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to instance variables.


    Reading the JLS is a useful way to resolve syntax questions.

    --
    Lew
     
    Lew, May 30, 2007
    #6
  7. Lew Guest

    Joshua Cranmer wrote:
    > Jussi Piitulainen wrote:
    >> writes:
    >>> Jussi Piitulainen wrote:
    >>>> Out.java:6: illegal forward reference
    >>>> x = k; //invalid
    >>>> ^
    >>>> 1 error
    >>> so "forward assignment" is ok?
    >>>
    >>> k = 5; //"forward assignment"
    >>> x = k; //compiler error because of forward reference
    >>>
    >>> ?

    >>
    >> Seems so. A simplest web search suggests that the JLS, possibly in
    >> section 8.3.2, requires declaration before using a variable in an
    >> initializer "not on the left hand side of an assignment".

    >
    > I suppose that makes sense.
    >
    > k = 5; is correct because the assignment of k works, but if x=k; were
    > allowed, this could happen:
    >
    > int x;
    > {
    > x = k;
    > }
    > int k = x;
    >
    > What is x and k? k is defined initially in terms of x, which is
    > initialized to k, so no definite assignment can be given them.
    >
    > In short, forward assignment is harmless, so it is allowed; forward
    > reference opens up a can of worms, so it is forbidden.


    Skipping the web search and going straight to the JLS we find:
    <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2>
    > Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to class variables.


    and
    > The declaration of a member needs to appear textually 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:
    >
    > * The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
    > * The usage is not on the left hand side of an assignment.
    > * The usage is via a simple name.
    > * C is the innermost class or interface enclosing the usage.
    >
    > A compile-time error occurs if any of the four requirements above are not met.


    I interpret that last sentence to implicitly include "and the usage is a
    forward reference."

    Instance initializers are covered in:
    <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.6>
    which repeats:
    > Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to instance variables.


    The JLS can be very useful in resolving Java syntax or semantics questions.

    --
    Lew
     
    Lew, May 30, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pls send me dotnet faq's

    , Sep 19, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    955
  2. Pial
    Replies:
    4
    Views:
    57,058
    Phatman
    Aug 27, 2010
  3. Alistair Lamb

    CLOCK__SIGNAL constraint! pls help

    Alistair Lamb, Mar 24, 2005, in forum: VHDL
    Replies:
    1
    Views:
    1,621
    Tim Good
    Mar 24, 2005
  4. Ravi

    pls explain this macro

    Ravi, Jul 18, 2007, in forum: C Programming
    Replies:
    4
    Views:
    371
    santosh
    Jul 18, 2007
  5. roohbir

    Can anybody explain this code pls?

    roohbir, Oct 23, 2006, in forum: Javascript
    Replies:
    2
    Views:
    114
    Evertjan.
    Oct 23, 2006
Loading...

Share This Page