No. Each object has its own identity, so there is no confusion about which variables are which. Each object's constructor gave its variables the correct values.
If this is confusing, remember that object oriented programming is supposed to
immitate the real world.
Think of some objects of the real world, say objects of the class Human.
Each object has its own identity (ie. Bob is a different individual from Jill)
even though each has parts that have the same name.
It is not confusing to talk of "Bill's heart" and "Bill's nose," and "Jill's heart"
and "Jill's nose."
With "dot notation" this would be Bob.heart, Bob.nose, Jill.heart
, and
Jill.nose
.
Below is a slightly different version of the program.
class Car { . . . . } class MilesPerGallon { public static void main( String[] args ) { Car car = new Car( 32456, 32810, 10.6 ); System.out.println( "Miles per gallon of car 1 is " + car.calculateMPG() ); car = new Car( 100000, 100300, 10.6 ); System.out.println( "Miles per gallon of car 2 is " + car.calculateMPG() ); } }
How does this program differ from the previous program?