• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the objects reference name if created in an array?

 
jon ninpoja
Ranch Hand
Posts: 199
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i had a bunch of horses and stats instantiated from my class
then i was reading in head first java that this was a better way to create/manipulate objects in an array



now my question is...in the array at index 0 i have created a Horse object...but what is it called? does it only belong to the array?
what if i wanted to use the horse i created outside of the array?...am i making any sense here?

 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's "called" horsePool1[0]. If you want to "name" it with a different variable, you can just assign a reference to it to a variable:

You can then reference the object using both horsePool1[0] and theHorse.

A few more remarks:
  • Don't use arrays when you can use collections instead. In this case, you could just use a List.
  • Don't call an empty constructor if you have to set properties explicitly afterward. Just set the properties in a constructor that initializes all properties.
  • Try to create immutable classes.

  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic