Simple question

Discussion in 'Java' started by ankur, Nov 9, 2008.

  1. ankur

    ankur Guest

    I sure am missing something very simple here. This simple piece of
    code compiles without error :

    public class SampleClass {

    int j = 10*this.r;

    int r;

    }


    But this does not:

    public class SampleClass {


    int j = 10*r;

    int r;

    }

    Why does first one compile fine and not the second one. You see there
    is only one r in the class.

    Ankur
    ankur, Nov 9, 2008
    #1
    1. Advertising

  2. On Nov 9, 9:12 pm, ankur <> wrote:
    > I sure am missing something very simple here. This simple piece of
    > code compiles without error :
    >
    > public class SampleClass {
    >
    > int j = 10*this.r;
    >
    > int r;
    >
    > }
    >
    > But this does not:


    Can you give us the compiler's name and error message?
    softwarepearls_com, Nov 9, 2008
    #2
    1. Advertising

  3. ankur wrote:
    > I sure am missing something very simple here. This simple piece of
    > code compiles without error :


    [ Reformatting for readability. ]
    > public class SampleClass {
    > int j = 10*this.r;
    > int r;
    > }


    > But this does not:
    > public class SampleClass {
    > int j = 10*r;
    > int r;
    > }


    Section §8.3.2.3:
    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.

    What this means is that if we want to use a forward reference, we have
    to ensure that we break one of those requirements. Here our are options:

    Option 1: Forward references are permissible in constructor bodies.
    Option 2: Forward references are permissible on the lhs of an assignment
    (e.g. int j = r = 10) [1].
    Option 3: Forward references are permissible if you do it via a
    qualified name (e.g., int j = this.r * 10).
    Option 4: Forward references are permissible within inner classes.

    The first class does option 3, but the second class uses none of them.

    [1] Pop quiz! Does this compile? Justify your answer with citations from
    the JLS.
    class Test {
    int j = (r = 10) + 1;
    int k = r++
    int r;
    }

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Nov 9, 2008
    #3
  4. ankur

    ankur Guest

    On Nov 9, 1:05 pm, Joshua Cranmer <> wrote:
    > ankur wrote:
    > > I sure am missing something very simple here. This simple piece of
    > > code compiles without error :

    >
    > [ Reformatting for readability. ]
    >
    > > public class SampleClass {
    > >   int j = 10*this.r;
    > >   int r;
    > > }
    > > But this does not:
    > > public class SampleClass {
    > >   int j = 10*r;
    > >   int r;
    > > }

    >
    > Section §8.3.2.3:
    > 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.
    >
    > What this means is that if we want to use a forward reference, we have
    > to ensure that we break one of those requirements. Here our are options:
    >
    > Option 1: Forward references are permissible in constructor bodies.
    > Option 2: Forward references are permissible on the lhs of an assignment
    > (e.g. int j = r = 10) [1].
    > Option 3: Forward references are permissible if you do it via a
    > qualified name (e.g., int j = this.r * 10).
    > Option 4: Forward references are permissible within inner classes.
    >
    > The first class does option 3, but the second class uses none of them.
    >
    > [1] Pop quiz! Does this compile? Justify your answer with citations from
    > the JLS.
    > class Test {
    >    int j = (r = 10) + 1;
    >    int k = r++
    >    int r;
    >
    > }
    >
    > --
    > Beware of bugs in the above code; I have only proved it correct, not
    > tried it. -- Donald E. Knuth


    The code will not compile because forward reference for r is used on
    the RHS in line 3 and it is using the simple name not a qualified
    name ! Thanks for the answer.

    Ankur
    ankur, Nov 9, 2008
    #4
  5. ankur wrote:
    > On Nov 9, 1:05 pm, Joshua Cranmer <> wrote:
    >> [1] Pop quiz! Does this compile? Justify your answer with citations from
    >> the JLS.
    >> class Test {
    >> int j = (r = 10) + 1;
    >> int k = r++
    >> int r;
    >>
    >> }

    >
    > The code will not compile because forward reference for r is used on
    > the RHS in line 3 and it is using the simple name not a qualified
    > name ! Thanks for the answer.


    You pass!

    [ Side note: it is good form to not quote signatures. ]

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Nov 9, 2008
    #5
  6. ankur

    Tom Anderson Guest

    On Sun, 9 Nov 2008, Joshua Cranmer wrote:

    > ankur wrote:
    >> I sure am missing something very simple here. This simple piece of
    >> code compiles without error :

    >
    > [ Reformatting for readability. ]
    >> public class SampleClass {
    >> int j = 10*this.r;
    >> int r;
    >> }

    >
    >> But this does not:
    >> public class SampleClass {
    >> int j = 10*r;
    >> int r;
    >> }

    >
    > Section §8.3.2.3:
    > 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.
    >
    > What this means is that if we want to use a forward reference, we have to
    > ensure that we break one of those requirements. Here our are options:
    >
    > Option 1: Forward references are permissible in constructor bodies.
    > Option 2: Forward references are permissible on the lhs of an assignment
    > (e.g. int j = r = 10) [1].
    > Option 3: Forward references are permissible if you do it via a qualified
    > name (e.g., int j = this.r * 10).
    > Option 4: Forward references are permissible within inner classes.
    >
    > The first class does option 3, but the second class uses none of them.
    >
    > [1] Pop quiz! Does this compile? Justify your answer with citations from the
    > JLS.
    > class Test {
    > int j = (r = 10) + 1;
    > int k = r++
    > int r;
    > }


    I'm guessing that the definition of j is fine, but k is no good becuase
    "r++" counts as a simple name on the RHS - as well as a simple name on the
    LHS, which is okay.

    Which means that you could write (being needlessly complex):

    int k = (r = this.r + 1) ;

    And it would be okay (although this would be a preincrement, not a
    postincrement). Whereas:

    int k = (r = r + 1) ;

    Wouldn't. Amirite? Ick.

    tom

    --
    Outnumbered but never outgunned.
    Tom Anderson, Nov 9, 2008
    #6
  7. Tom Anderson wrote:
    > Which means that you could write (being needlessly complex):
    >
    > int k = (r = this.r + 1) ;


    Or you could just do:
    int k = this.r++;

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Nov 9, 2008
    #7
  8. ankur

    Tom Anderson Guest

    On Sun, 9 Nov 2008, Joshua Cranmer wrote:

    > Tom Anderson wrote:
    >> Which means that you could write (being needlessly complex):
    >>
    >> int k = (r = this.r + 1) ;

    >
    > Or you could just do:
    > int k = this.r++;


    Oh good god, man! Am i quite surrounded by vulgarians? :)

    tom

    --
    Someone may observe that no doubt the conclusion preceded the
    'proofs'. For who gives himself up to looking for proofs of something
    he does not believe in, or the predication of which he does not care
    about? -- Jorge Luis Borges
    Tom Anderson, Nov 11, 2008
    #8
    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. Replies:
    0
    Views:
    550
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    656
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    835
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    367
    Roland Hall
    Jan 26, 2004
  5. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    211
    Peter Bailey
    Apr 8, 2008
Loading...

Share This Page