COMP SCI 302 Chapter 9: Chapter 9

44 views2 pages

Document Summary

The compiler does not allocate memory for class/field variables until an object is created of that class type. Constructors the constructor has the same name as the class. The constructor method has no return type, not even void. A programmer specifies the constructor that should be called upon creating an object. For example, the statement runnerinfo runner1 = new runnerinfo(); creates a new object of type runnerinfo using the constructor runnerinfo(). Runnerinfo object will be initialized to zeros by the constructor. A constructor that can be called without any arguments is called a default constructor. If a class does not have a programmer-defined constructor, then the java compiler implicitly defines a default constructor that automatically initializes all fields to their default values. Carperformance car2 = new carperformance(0. 0, 60. 0); creates a carperformance object with the currspeed field initialized to 60. 0 and the curraccel field initialized to 0. 0.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions