3.4.2 Class AccountTest: Initializing Account Objects When They’re Created

The AccountTest program (Fig. 3.6) initializes two Account objects using the constructor. Line 10 creates and initializes the Account object account1. Keyword new requests memory from the system to store the Account object, then implicitly calls the class’s constructor to initialize the object. The call is indicated by the parentheses after the class name, which contain the argument "Jane Green" that’s used to initialize the new object’s name. The class instance creation expression in line 10 returns a reference to the new object, which is assigned to the variable account1. Line 11 repeats this process, passing the argument "John Blue" to initialize the name for account2 ...

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.