|
|
Language Fundamentals - Constructor Declarations
Syntax (JLS §8.8)
modifiers ClassName(arguments) throwsClause {
// Constructor body
}
The modifers, ClassName, arguments, and throwsClause are optional.
[Note: all are optional in the sense
that your class does NOT have to declare a constructor (see following on default constructors);however, if you do include a constructor modifiers, arguments and the throws clause are optional.]
| Note |
|
A method having the same name as the class is not treated as a constructor
public void MyClassName() {} // not a constructor
public MyClassName() {} // constructor
A constructor cannot have a return type.
|
Tips
- a constructor body can include a return statement providing no value is returned
Traps
- subclass with default constructor when the superclass does not have a no-args constructor or it's no-arg constructor has a throws clause
- constructor declared with a return type
|