3.4.1 Declaring an Account Constructor for Custom Object Initialization

When you declare a class, you can provide your own constructor to specify custom initialization for objects of your class. For example, you might want to specify a name for an Account object when the object is created, as in line 10 of Fig. 3.6:

Account account1 = new Account("Jane Green");

In this case, the String argument "Jane Green" is passed to the Account object’s constructor and used to initialize the name instance variable. The preceding statement requires that the class provide a constructor that takes only a String parameter. Figure 3.5 contains a modified Account class with such a constructor.

Account Constructor Declaration

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.