• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instance Initializer expressions

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic