This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

instance variable with the same name as the class?  RSS feed

 
Ranch Hand
Posts: 180
  • 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 ]
 
Bartender
Posts: 2856
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: 180
  • 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
 
Java Cowboy
Sheriff
Posts: 16084
88
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.
 
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!