1 / 10

Lecture 4: Objects & Fields

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

aldona
Download Presentation

Lecture 4: Objects & Fields

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 4:Objects & Fields

  2. 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

  3. 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

  4. 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 */

  5. 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;

  6. 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;

  7. 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);} }

  8. 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);}

  9. 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;

  10. 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

More Related