Java Reference
In-Depth Information
8.8.9. Default Constructor
If a class contains no constructor declarations, then a default constructor with no formal
parameters and no throws clause is implicitly declared.
If the class being declared is the primordial class Object , then the default constructor has an
empty body. Otherwise, the default constructor simply invokes the superclass constructor
with no arguments.
It is a compile-time error if a default constructor is implicitly declared but the superclass
does not have an accessible constructor (§ 6.6 ) that takes no arguments and has no throws
clause.
In a class type, if the class is declared public , then the default constructor is implicitly given
the access modifier public 6.6 ) ; if the class is declared protected , then the default construct-
or is implicitly given the access modifier protected 6.6 ); if the class is declared private , then
the default constructor is implicitly given the access modifier private 6.6 ); otherwise, the
default constructor has the default access implied by no access modifier.
In an enum type, the default constructor is implicitly private 8.9.2 ) .
Example 8.8.9-1. Default Constructors
The declaration:
public class Point {
int x, y;
}
is equivalent to the declaration:
public class Point {
int x, y;
public Point() { super(); }
}
where the default constructor is public because the class Point is public .
Example 8.8.9-2. Accessibility of Constructors v. Classes
The rule that the default constructor of a class has the same access modifier as the
class itself is simple and intuitive. Note, however, that this does not imply that the
constructor is accessible whenever the class is accessible. Consider:
Search WWH ::




Custom Search