Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward reference doubt

 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class NonStaticInitializers {
int length=10;
double area=length*width;//illegal
//double area=length*this.width;//legal
int width=10;
int sqside=height=20;//legal
int height;
}
i am not able to understand this code` .can anybody please explain it to me?what makes this.width legal?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not considered a forward reference if you say this.weight because you're not accessing the variable through its simple name.
 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

int sqside=height=20;//legal


The forward referencing rule says that you cannot use a
variable on the right hand side of an assignment statement
or use it to print something before it can be declared.

since height is on the left hand side, it wont create any problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic