• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance Initializer and Forward Referancing

 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the error coming in Forward Referancing
done in the Instance Initializer used in the nested class
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear sivani,
Think this way :
in class A hg and lf are instance variable
however in class B these are local variable.
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variable vs.local variables - wat are the differences in the rules for forward referencing for them?
 
Kevin Vicy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int hg = lf = 90; // declaration for 'hg' not for 'lf'
int lf; // only here 'lf' is declared

this member variables declaration can also be done as follows

int lf;
int hg = lf = 90;

this type of declaration ordering is not an issue with member variables but matters for local variable declaration. Hope this helps!
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear Shivani,
rules for local variable is "variable must be declared before accessing it" and one can't make a forward reference to local variable.

Hope this helps you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic