This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes variable in place of object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "variable in place of object?" Watch "variable in place of object?" New topic
Author

variable in place of object?

Wayne Todd
Greenhorn

Joined: Oct 05, 2004
Posts: 2
I have a simple object called car:

public class Car{
int numMiles;
int year;

//default constructor
Car(){
System.out.println("Car created");
}

My question is this: If instantiate a number of cars (car1, car2, .., car100), is there any way for a user to nominate a particular car and modify its numMiles. This obviously won't work:

String myCar;
//user selects the car somehow but for here lets say
myCar="Car1";
myCar.numMiles=4000;
=====================================================
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I'm not sure I'm following, but it sounds like you might want to create an Array of Car objects. Then you could access a particular Car by its index number in the Array.



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Wayne Todd
Greenhorn

Joined: Oct 05, 2004
Posts: 2
Perfect Marc, thank you for taking the time.
Wayne
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Or... :roll:

 
 
subject: variable in place of object?
 
Similar Threads
Just was typing a LinkedList code from 'Programming With Java'
Pointers?
Using final
hashCode and equals implementation
Difference between Instance, Object and reference