When you declare a local variable within method using the same name and type like instance variable of – does it mean the instance variable becomes ‘invisible’ inside that method?
public class Test {
Card card;
public void foo(){
Card card = new Card();
card.test();
}
}
So I’m declaring and instantiating local variable card in foo() method. And then test() method is called for local variable. If I removed the Card card = new Card(); test() method is invoked for instance variable.
What you are seeing is sometimes referred to as “shadowing” the variable. Any time a variable is declared within an inner scope, the variable becomes the default variable tied to that name until it goes out of scope.
In this case, you could access the class variable using syntax: