• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unique object names  RSS feed

 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found out what my next java class project is, and i am trying to get a head start on it. It is a simple address book. it needs to have 4 choices
that get cycled through until quit is chosen:
1. add business contact
2. add personal contact
3. display contacts
4. quit

I have made an abstract contact class with a personal subclass and business subclass(all 3 are required). they have all been made, with
set/get methods(the wonders of encapsulation and polymorphism have finally hit home!). The main program needs to add a new subclass
object when chosen, and it needs to
accept and store contacts by type


So, i am figuring 2 object arraylists for business/personal.

My question is: how do you create an object without having a name for it already programmed? Does it even need to have a unique name,
since it would be stored in a seperate index and/or arraylist?



Then cycle through, changing the properties, and add again to personalArray?
This is blowing my mind.
 
Kondwani Chipeta
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Matthew,

My question is: how do you create an object without having a name for it already programmed? Does it even need to have a unique name,
since it would be stored in a seperate index and/or arraylist?


The answer to this is that you simply cannot create two separate objects using the same reference variable.
e.g. If you were to do this:



The compiler would simply tell you that the reference variable c has already been defined.


However you can do something like this:


In this case the reference variable c points to a new object on the heap and the object that it was first referencing gets garbage collected.

If you want to use an array of Contacts, you can simply declare an array of whatever size you want and then intialize the variable using a loop.
e.g.


Cheers.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why bother with a temporary variable at all?

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Joseph wrote:My question is: how do you create an object without having a name for it already programmed? Does it even need to have a unique name,
since it would be stored in a seperate index and/or arraylist?

Objects don't have names. Variables have names. When you do this:

then you are not creating an object named 'contact' - you are creating a variable named 'contact' that refers to an object of type Contact.

Note that you can have multiple variables that refer to the same object:
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, when you do it that way, does it automatically put the strings into the properties in the order theyve been written? example:



Then inputting:


Would i then be able to use my methods to return specific values?

output== Matt
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you need to have the appropriate constructor.

But pay special attention to Jesper's post. Each object does not need to have a unique variable.
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case the reference variable c points to a new object on the heap and the object that it was first referencing gets garbage collected.

Kondwani, if its added to an arraylist, does the arraylist keep pointing, or does it get thrown away too?
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am i getting this right jesper?
You can keep recycling the variable Contact a, but once it has been added to the arraylist, it keeps the correct properties.
Then i can just loop through the arraylist, grabbing the properties you need.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One important thing to understand about variables is that variables are not the objects themselves, they are references to objects. We have a Campfire Story that explains it: Cup Size -- a story about variables.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java won't garbage collect your objects until it decides that you can't access them anymore. You don't need to worry about exactly how it does it, but it won't garbage collect objects in an ArrayList or (almost) any other container until it decides to garbage collect the container itself.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects don't get garbage collected once there are references to them which are located within a live thread of execution...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!