File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring 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!