Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance Initializer expressions  RSS feed

 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I have gone through a statement saying "declare before you read".

I have the following lines of code:

case 1:
int j = i;//compile time error.
int i;

case 2:
int j = this.i;//compiles fine
int i;

case 3:
int j = i = 20;//compiles fine.
int i;

I have the following questions:
Q 1:
While accessing the instance variables the compiler will append this keyword before the variable. Is my assumption correct.

Q 2:
Can any one explain why the compiler does not throw any error in case 3 even if we are not using this keyword before the variable?

Thanks,
Ravindra.
 
Michael Jennings
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you check your code.

You cannot have repeating declarations of i and j within the same block.

Try keeping just one case and commenting out the others, and you will find that everything works well.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!