aspose file tools*
The moose likes Beginning Java and the fly likes  instance variable with the same name as the class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " instance variable with the same name as the class?" Watch " instance variable with the same name as the class?" New topic
Author

instance variable with the same name as the class?

Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
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?

Varuna

[ December 11, 2008: Message edited by: Varuna Seneviratna ]
[ December 11, 2008: Message edited by: Varuna Seneviratna ]

Varuna Seneviratna
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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.


Do you mean something like
Student Student= new Student();


SCJP, SCWCD.
|Asking Good Questions|
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
Originally posted by Amit Ghorpade:


Do you mean something like
Student Student= new Student();


No I mean inside the class like
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Chadd Franck
Ranch Hand

Joined: Nov 05, 2008
Posts: 50
As far as I can tell, Java requires a type for every variable that is declared

ie, String someString = new String();

String is the TYPE of variable;

someString is the variable TYPE String;

= 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instance variable with the same name as the class?