Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instance variable with the same name as the class?

 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic