It is legal to declare a local variable with the same name as an instance variable; this is called "shadowing". : Method Variable « Java Source And Data Type « SCJP






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








1.21.Method Variable
1.21.1.Local (Automatic/Stack/Method) Variables
1.21.2.local variables can be marked final.
1.21.3.Local variable has its scope limited to the method who declares it
1.21.4.It is legal to declare a local variable with the same name as an instance variable; this is called "shadowing".
1.21.5.Attempt to use a loop variable outside the code block of the loop.
1.21.6.Local Object References
1.21.7.Shadow an instance variable by declaring a local variable of the same name directly or as part of an argument
1.21.8.Shadowed variable is an object reference