• 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

Reference Variable and Instance Variable

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one confusion. What the relation between Int a and Int b(instance variables) with the reference variables a and b of serialization2 object. Because code is not giving any compilation error.



Thanks in advance
Vinay
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have varibles of the same name as instance/class variables inside a method. The variables inside the method are used if you do not say differently. And to use the instance/class variables insted you use the "this" keyword.

Please read:
http://download.oracle.com/javase/tutorial/java/javaOO/thiskey.html
 
Ranch Hand
Posts: 47
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Vinay,

The concept which you are looking for is called as Shadowing. As per JLS it is legal to declare two variables with same name but having different scope (in that i think one scope should always be class level scope).

In your code also you are declaring two variables with same name but having different scopes. First is an int having class level scope and other is an object reference variable having method scope. By doing this you are shadowing you instance variable with your local variable in the method.

Even it is legal to declare two variables of same type but different scopes.

Thanks.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

himanshu.harish agrawal wrote:... The concept which you are looking for is called as Shadowing. As per JLS it is legal to declare two variables with same name but having different scope...


That's exactly right. If interested, here are the references:

JLS 14.4.2 - Scope of Local Variable Declarations
JLS 14.4.3 - Shadowing of Names by Local Variables

(It's a good idea to bookmark the JLS. )
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic