<< Chapter < Page Chapter >> Page >

Program: Constructor01B.java

// Learning Object Constructor01B //    what are constructors for?class Song {     String name;    int seconds;     double pricePerSecond;      Song() {        name = "Waterloo";         seconds = 164;        pricePerSecond = 0.01;     }      public double computePrice() {        return seconds * pricePerSecond;     }}  public class Constructor01B {     public static void main(/*String[] args*/) {         Song song1 = new Song();        double price = song1.computePrice();     }}

This program is the same as the previous one except that the assignment of nondefault values to the fields of the object is moved to an explicit constructor.

  • The variable song1 is allocated and contains the null value.
  • Memory is allocated for the fields of the object; this is displayed in the Instance and Array Area. Default values are assigned to the three fields.
  • The constructor is called and assigns values to the three fields; then it returns a reference to the object.
  • The reference is stored in the variable song1 .
  • The reference in song1 is used to call the method computePrice on the object; the method computes and returns the price, which is assigned to the variable price .

Exercise Add the creation of a second object song2 to the program and verify that it is initialized to the same values.

Concept Of course, it is highly unlikely that all objects created from a class will be initialized with the same values. A constructor can have formal parameters like any other method and is called with actual parameters.

Program: Constructor01C.java

// Learning Object Constructor01C //    what are constructors for?class Song {     String name;    int seconds;     double pricePerSecond;      Song(String n, int s, double p) {        name = n;         seconds = s;        pricePerSecond = p;     }      public double computePrice() {        return seconds * pricePerSecond;     }}  public class Constructor01C {     public static void main(/*String[] args*/) {         Song song1 = new Song("Waterloo", 164, 0.01);        double price = song1.computePrice();     }}

This program is the same as the previous one except that the constructor has formal parameters and the actual parameters passed to the constructor are assigned to the fields of the object.

  • The variable song1 is allocated and contains the null value.
  • Memory is allocated for the fields of the object; this is displayed in the Instance and Array Area. Default values are assigned to the three fields.
  • The constructor is called with three actual parameters; these values are assigned to the formal parameters of the constructor method.
  • The values of the formal parameters are assigned to the three fields; then the constructor returns a reference to the object.
  • The reference is stored in the variable song1 .
  • The reference in song1 is used to call the method computePrice on the object; the method computes and returns the price, which is assigned to the variable price .

Exercise Modify the class so that the second parameter passes the number of minutes; the value of the field seconds will have to be computed in the constructor.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Learning objects for java (with jeliot). OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10915/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?

Ask