It changes the value of the 'Color' property of the Car
instance stored in the table; a new value is not created.
You don't show a type for myIntHashTable
. Note that java.util.Map is typically preferred over the legacy java.util.Hashtable. The latter does locking.
Also, are you indexing these cars by consecutive integral values? If so, you might want some form of List
.
You probably want something like this:
final Map<Integer, Car> cars = new HashMap<Integer, Car>();
final Car someCar = new Car();
cars.put(1, someCar);
final Car carInTable = cars.get(1). // refers to same object as someCar
carInTable.setColor(red);
A List
implementation is similar:
final List<Car> cars = new ArrayList<Car>();
final Car someCar = new Car();
cars.add(someCar);
final Car carInTable = cars.get(1). // refers to same object as someCar
carInTable.setColor(red);
Sans Generics, just do the casting yourself:
final Map cars = new HashMap();
final Car someCar = new Car();
cars.put(1, someCar);
final Car carInTable = (Car) cars.get(1). // refers to same object as someCar
carInTable.setColor(red);
final List cars = new ArrayList();
final Car someCar = new Car();
cars.add(someCar);
final Car carInTable = (Car) cars.get(1). // refers to same object as someCar
carInTable.setColor(red);
You can always compare object references with ==
when in doubt about whether two objects are the same object:
System.println.out("someCar and carInTable are " + ((someCar == carInTable) ? "" : "not ") + "the same object");