O'Reilly logo

Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition by Rolf W. Rasmussen, Khalid A. Mughal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

4.4. Constructors

The main purpose of constructors is to set the initial state of an object when the object is created by using the new operator.

A constructor has the following general syntax:

<accessibility modifier> <class name> (<formal parameter list>)            <throws clause> // Constructor header  { // Constructor body    <local variable declarations>    <nested local class declarations>    <statements>  }

Constructor declarations are very much like method declarations. However, the following restrictions on constructors should be noted:

  • Modifiers other than an accessibility modifier are not permitted in the constructor header. Accessibility modifiers for constructors are discussed in Section 4.7 on page 131.

  • Constructors cannot return ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required