Simple question

A

ankur

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
 
S

softwarepearls_com

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?
 
J

Joshua Cranmer

ankur said:
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;
}
 
A

ankur

ankur said:
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;

}

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
 
J

Joshua Cranmer

ankur said:
[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. ]
 
T

Tom Anderson

ankur said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top