This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Instance Initializer expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance Initializer expressions" Watch "Instance Initializer expressions" New topic

Instance Initializer expressions

Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
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?

Michael Jennings

Joined: Jul 04, 2007
Posts: 20
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.
I agree. Here's the link:
subject: Instance Initializer expressions
It's not a secret anymore!