Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Simple question

Reply
Thread Tools

Simple question

 
 
ankur
Guest
Posts: n/a
 
      11-09-2008
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
 
Reply With Quote
 
 
 
 
softwarepearls_com
Guest
Posts: n/a
 
      11-09-2008
On Nov 9, 9:12*pm, ankur <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      11-09-2008
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
 
Reply With Quote
 
ankur
Guest
Posts: n/a
 
      11-09-2008
On Nov 9, 1:05*pm, Joshua Cranmer <(E-Mail Removed)> 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
 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      11-09-2008
ankur wrote:
> On Nov 9, 1:05 pm, Joshua Cranmer <(E-Mail Removed)> 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
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      11-09-2008
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.
 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      11-09-2008
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
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      11-11-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plz, a simple answer to a simple question about IP addresses MeekiMoo Computer Support 0 07-28-2009 08:10 AM
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments