Win a copy of Event Streams in Action this week in the Java in General 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Do object reference names need to correspond to the actual objects?

 
Ranch Hand
Posts: 50
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lets say you have a class called student

and you want to instantiate x amount of student objects

and you obviously have a name for your student like mary jane etc stored in an instance variable studentName

and you create objects



does it make sense to create the objects like this rather



will it make it much easier to keep track of which object is which? or doesnt it really matter.

i'm sorry if this is a stupid question.







 
Rancher
Posts: 117
3
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, there are no silly questions.  

Secondly, it is my opinion that you should name your objects in a way that makes sense (not only for you but also for other developers that might be working on your code after you).

For instances of class Student, I would also use a name, rather than student1, student2, etc (even though in your example all instances are currently alike - however, it is a good habit to get into right from the beginning)
 
Sheriff
Posts: 13553
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the rule of thumb: choose names that reflect the intent. Names like what you suggested don't really follow that guideline. However, you give very little context around how those names would be used. Would the object that you named mary always represent a student named Mary? Something like a name is usually an attribute of the object instead of a variable name.

I guess it also depends on what kind of abstraction you're trying to represent in your code.

For example, this might be more appropriate:
 
lowercase baba
Posts: 12753
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my concern with using "mary" is that there is nothing to prevent you from doing this:



Now both "mary" and "ruth" point to the same object...but which is it?

is there a reason you need a reference to each one? Why not use a collection (array, list, etc)?  
 
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to create 'x' amount of Students then you will most likely end up with an array or List of Student objects, in which case each object will need to know who it refers to because the variable name won't tell you.

 
Marshal
Posts: 65041
247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are creating an array, I always like to try array initlaisers, like this:-
 
warren brandt
Ranch Hand
Posts: 50
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all!
 
Campbell Ritchie
Marshal
Posts: 65041
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

warren brandt wrote:Thank you all!

That's a pleasure
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!