• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variable in place of object?

 
Wayne Todd
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Wayne Todd
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect Marc, thank you for taking the time.
Wayne
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or... :roll:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic