Friday, 16 January 2015

Shadowing in java


Shadowing means declare a local variable with the same names as instance variable

class Test
{
int count=9;   //instance variable
public void logIn()
 {
   int count=10;  //local variable
 System.out.println(“ local variable count”+count);
}
public void count()
{
  System.out.println(“ local variable count”+count);
}
public static void main(String args[])
 {
   new Test().logIn();
   new Test().count();
}

output:
local variable =10
Instance=9

No comments:

Post a Comment