Lecture3

Lecture3 - Initializing instance variables Declared in...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Initializing instance variables c: Declared in class . class Car { String licensePlate = "543 A32"; double speed = 100.0; // in kilometers per hour double maxSpeed = 120.0; // in kilometers per hour } class CarTest2 { public static void main(String[] args) { Car c = new Car(); System.out.println(c.licensePlate + " is moving at " + c.speed + "kilometers per hour."); } } licencePlate: “ 543 A23 speed: 100.0 maxSpeed: 120.0
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 in main() C: class Car { String licensePlate; double speed; double maxSpeed; void SetlicensePlate( ) { ….. } void SetSpeed () { ….. } double GetSpeed () { …… } } class CarTest { public static void main(String[] args) { Car c = new Car(); c.licencePlate = "543 A23"; c.speed = 100.0; c.maxSpeed = 120; System.out.println(c.licencePlate); System.out.println(c.speed); System.out.println(c.maxSpeed); } } licencePlate: “ 543 A23 speed: 100.0 maxSpeed: 120.0
Background image of page 2
3 Basic class structure class ClassName { Variables Constructors Methods } Constructors Whenever you create an object, the system will always try to implement the constructor, assign the initial values to the instance variables. class CarTest{ public void static main(String args[]) Car c = new Car(); c: ……} class Car { String licensePlate; double speed; double maxSpeed; Car() { licensePlate = “543 A32”; speed = 100.0; maxSpeed = 120.0; } // accelerate to maximum speed // put the pedal to the metal void floorIt() { speed = maxSpeed; } } licencePlate: “ 543 A23 speed: 100.0 maxSpeed: 120.0
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Create a constructor that accepts arguments and use those to initialize the instance variable. c: d: class Car { String licensePlate; double speed; double maxSpeed; Car(String licensePlate1, double speed1, double maxSpeed1) { licensePlate = licensePlate1; speed = speed1; maxSpeed = maxSpeed1; } void floorIt() { speed = maxSpeed; } } class CarTest{ public void static main(String args[]) Car c = new Car(“543 A32”, 100.0, 120.0); c.floorIt(); Car d = new Car(“676 A66”, 120.0, 150.0); d.floorIt(); ……} licencePlate: “ 543 A23 speed: 100.0 maxSpeed: 120.0 licencePlate: “676 A66” speed: 120.0 maxSpeed: 150.0
Background image of page 4
5 Using Constructors A constructor has the same name as the class A constructor does not return a value it has no return type, not even void. You do not have to define a constructor for a class. Only when you would like to assign initial values to instance variable of each created object, you need to design a constructor in the class. If a class has no constructor, the compiler generates a default constructor with no arguments for the class. class Car { String licensePlate; double speed; double maxSpeed; Car() { licensePlate = “543 A32”; speed = 100.0; maxSpeed = 120.0; } } class CarTest2 { public static void main(String[] args) { Car c = new Car(); System.out.println(c.licensePlate+"is moving at " + c.speed + "kilometers per hour."); } }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 If a class has any constructor, the compiler will not generate the default constructor. If such a class requires a default constructor, its implementation must be provided. Any attempt to call the default constructor will be a compile time error if an explicit default constructor is not provided in such a case.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 47

Lecture3 - Initializing instance variables Declared in...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online