Explain why it is possible to have a instance variable with the same name as the name of a class.Please will you explain the issue for local variables and instance variables both.
Is it because:- It is possible because the class is a template which has no connection with data and a instance variable is a memory location which is used to store data in a different location,The stack?.If the instance variable is an object reference variable it resides in the Stack.But the object resides in the Heap.Which also has no connection to the class.
Where does the class template resides in memory?
[ December 11, 2008: Message edited by: Varuna Seneviratna ] [ December 11, 2008: Message edited by: Varuna Seneviratna ]
This is not because of some runtime thing (such as where objects or classes are stored in memory at runtime). Note that objects don't have names - variable names only exist in your source code, the compiler throws them away because the computer doesn't need variable names to run your program. It doesn't matter for the JVM what you name your variables.
The issue is that the compiler must be able to recognise if you mean class Student, or the variable named Student. Most of the time it can decide which of the two you mean, and if it can't, you'll get a compilation error.
= sets the variable and it's type to refrence what it's pointing to. In this case new String();
When you create a class ClassDog, you create it like this;
ClassDog newDog = new ClassDog();
ClassDog is the TYPE; newDog is the reference variable
Then when you create a instance variable with the same name, you don't declare it as the same TYPE but as as different TYPE ie;
String newDog = new String();
String is the Type; newDog is the reference variable.
This is why they can have the same reference variable name but and the machine doesn't get confused, Java references have two identifiers that must be called to identify a name. [ December 13, 2008: Message edited by: Chadd Franck ]