• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

forward reference

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I read JLS, and my understanding is this shoudl work:
class A{
{a=3;}
int a;
}
according to jls 1.2 edition, examples in chapter 8: Class.
but when I tried it, it didn't work. Compiler said
forward refercening is not allowed.
Can anyone help clarify this?
Thanks!
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You are trying to initialize the variable 'a' before declaring it. This is called forward referncing.So the compiler gives the error.

Thanks...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS section 8.3.2.3 spells out the only cases where forward referencing is not allowed, and you've created one of them.
 
anchal jain
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

look at the example code from jls chapter 8.2.3. static int
x is initialized in the static block before it's declared
later.
in initialization block, you can assign a data field
before the declaration.
and first look at this from jls 8.3.2.3:
********************************************************
8.3.2.3 Restrictions on the use of Fields during Initialization
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:
******************************************************

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.
C is the innermost class or interface enclosing the usage.
A compile-time error occurs if any of the three requirements above are not met.

class UseBeforeDeclaration {
static {
x = 100; // ok - assignment
int y = x + 1; // error - read before declaration
int v = x = 3; // ok - x at left hand side of assignment
int z = UseBeforeDeclaration.x * 2;
// ok - not accessed via simple name
Object o = new Object(){
void foo(){x++;} // ok - occurs in a different class
{x++;} // ok - occurs in a different class
};
}
{
j = 200; // ok - assignment
j = j + 1; // error - right hand side reads before declaration
int k = j = j + 1;
int n = j = 300; // ok - j at left hand side of assignment
int h = j++; // error - read before declaration
int l = this.j * 3; // ok - not accessed via simple name
Object o = new Object(){
void foo(){j++;} // ok - occurs in a different class
{ j = j + 1;} // ok - occurs in a different class
};
}
int w = x= 3; // ok - x at left hand side of assignment
int p = x; // ok - instance initializers may access static fields
static int u = (new Object(){int bar(){return x;}}).bar();
// ok - occurs in a different class
static int x;
int m = j = 4; // ok - j at left hand side of assignment
int o = (new Object(){int bar(){return j;}}).bar();
// ok - occurs in a different class
int j;
}

so, class A{
{a=1;}
int a;
}

should be legal, according to above excerpt.
 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
These lines are from JLS:


8.3.2.3 Restrictions on the use of Fields during Initialization

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:
1. The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
2. The usage is not on the left hand side of an assignment.
3. C is the innermost class or interface enclosing the usage.
A compile-time error occurs if any of the three requirements above are not met.

Last line is kind of killing me. I really don�t understand.



PLEASE DO HELP ME!
I really don�t understand what exactly these lines wants to explain??
Because if we look into following question
class A{
{a=3;}
int a;
}

because this question hold 1st condition but it does not hold 2nt. Because it is on the LHS and 2nd condition says if it not on LHS.


what I concluded here is if any of the three condition hold then declaration of the variable must come first before it is used by some expression �


please do help me here.
vivek

[This message has been edited by Vivek Shrivastava (edited August 14, 2000).]
 
anchal jain
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the same feeling...
if just innore the last sentence "a compile error...",
then my understanding is:
class A { {a=1;} int a;}
should be legal.
And also look at the example code. it commented
//OK on lines it thinks OK. Pay attention to
the static int x whose declaration is well behind
the static initialzation block where x=100; is enclosed.

Could anybody help? a million thanks!!!
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SomeOne please come forward. it would be a great help.
vivek
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, I posted the above link without really studying what it says carefully. It turns out that this JLS excerpt does not really answer any questions; in fact it creates more. The sentence "A compile-time error occurs if any of the three requirements above are not met" does indeed contradict the previous sentences, and renders the whole excerpt meaningless as it's impossible to tell what they really meant. I also agree that if we eliminate that sentence, then the code excerpt should be legal, because the usage is on the left side of an assignment operator (= sign). But this isn't the way the compiler behaves, so who knows what was really intended here? There doesn't seem to be any bug report for this, so I'll submit one now. Thanks for pointing this out, and sorry I didn't look more carefuly earlier.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I decided to submit an e-mail to [email protected], rather than a bug report (since the bug report form didn't have an option for bugs in the JLS itself). I'll report back if I get a response - and if nothing is heard in a few weeks, I'll put in the bug report as well.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code example most of the 'Ok' s are NOT OK. In fact when I compile the above code with jdk1.2.2 11 compiler errors occur which includes 5 'Ok's statements commented in the above code.
regds
maha anna
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, Deepak and Vivek discussed this once here.
The conclusion was that JDK1.3 does not comply with the JLS 2nd edition with respect to the rules for forward referencing.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But note that last time around, we didn't notice the line about "a compile-time error occurs if...". So there's the additional problem that the it's impossible to comply with the JLS2 as written, because it contradicts itself. Ah well...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic