Constructors

Objects are instances of classes that are created using constructors. Every class requires a constructor to create object instances. In fact, if a class does not declare a constructor, the compiler automatically creates a constructor for the class. However, in most cases, you will not rely on the compiler to create a constructor for you—you will declare your own constructors. The following is the syntax of a constructor declaration:

modifers ClassName(arguments) throwsClause {
 // Constructor body
}

The modifiers, arguments, and throws clause are optional. Valid modifiers are public, protected, private, or none. No modifier means that the constructor can only be accessed within the package where the class is declared. The public ...

Get Sun Certification Training Guide (310-025, 310-027): Java™ 2 Programmer and Developer Exams 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.