Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forward variable referencing.

 
amarkirt saroay
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

HFJS,Page 295 and 302,there is a clash in 2 scenarios of forward variable referencing.
Case one (Pg 295)

int doubleCount(){
count=count*2;
return count;
}
int count=1;


Case 2Page302)

void foo(){
int y=5+x;
int x=2;
}


In case one they say this forward referencing is allowed however in Case 2 its not allowed.Why?

 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is perfectly okay. Its interpreted correctly.

check out the forward reference rules taken from JLS spec

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.

 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here is an article to explain that
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic