100 likes | 232 Views
Lecture 4: Objects & Fields. Classes vs. Objects. Classes are blueprints describing data type On their own, classes (usually) cannot do anything Objects are instances of a class New objects created ( instantiated ) using new Fields describe state of an object
E N D
Classes vs. Objects • Classes are blueprints describing data type • On their own, classes (usually) cannot do anything • Objects are instances of a class • New objects created (instantiated) using new • Fields describe state of an object • Object’s behavior represented by methods
Instance Variables • All of class's instances have same fields… • … but values can differ between each instance • In a class, each field must have unique name • Different classes can duplicate names of fields • Field declaration must include data type • Will act like variables of that type • Can be primitive, enum, or reference type
Class Example public class Car {/** What kind of car & who made it */private String makeAndModel;/** Color of the car. */private String color;/** Percent full the gas tank is */private float tankLevel;/** Miles recorded on the odometer */private intodometerReading;/* Definition continues from here */
Using Fields (1) Car profHertzCar= new Car();profHertzCar.makeAndModel= “BMW Z4”;profHertzCar.color= “Deep Green Metallic”;profHertzCar.tankLevel= 1.0;profHertzCar.odometerReading= 10000;CaractualCar = new Car();actualCar.makeAndModel = “Subaru Outback";actualCar.color = “Brown”;actualCar.tankLevel = 0.0001;actualCar.odometerReading = 67634;
Using Fields (2) Car dreamCar = new Car();dreamCar.makeAndModel = “BMW Z4”;dreamCar.color = “Deep Green Metallic”;dreamCar.tankLevel = 1.0;dreamCar.odometerReading = 10000;CarrealCar = dreamCar;realCar.makeAndModel = “Subaru Outback”;realCar.color = “Silver”;realCar.tankLevel = 0.0001;realCar.odometerReading = 67634;
Code Example public class Player {public String name;public static voidmain(String[] args){Player player1, player2, player3; player1 = new Player();player1.name= "Homer";player2 = new Player();player2.name= “JJ”;player3 = player2;player3.name = "Hertz";System.out.print(player2.name+" "+player1.name);} }
Tracing With Objects & Fields public staticvoid main(String[] args) {Player player1, player2, player3; player1 = new Player();player1.name= "Homer”;player2 = new Player();player2.name= “JJ”; player3 = player2;player3.name = "Hertz";System.out.print(player2.name+" "+player1.name);}
Instance Variables Review • Each instance gets own copy of every field • Must specify instance whose field should be used • If you have instance, can set value at any time • No limit on use in code, just need the instance • Primitive or reference okay, like with any variable • Declare in class using:typename;
For Next Lecture • Keep reviewing your Java lessons • Will be discussing methods & constructors Friday • Really need to be back up-to-speed at that point • There is weekly assignment problem on Angel • Due next Tuesday at 5PM but could start earlier • With this finish removing rust from lazy summer