10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CLASSES Constructor Modifiers 8.8.3<br />

8.8.2 Constructor Signature<br />

It is a compile-time error to declare two constructors with override-equivalent<br />

(§8.4.2) signatures in a class. It is a compile-time error to declare two constructors<br />

whose signature has the same erasure (§4.6) in a class.<br />

8.8.3 Constructor Modifiers<br />

ConstructorModifiers:<br />

ConstructorModifier<br />

ConstructorModifiers ConstructorModifier<br />

ConstructorModifier: one of<br />

Annotation public protected private<br />

<strong>The</strong> access modifiers public, protected, and private are discussed in<br />

§6.6. A compile-time error occurs if the same modifier appears more than once in<br />

a constructor declaration, or if a constructor declaration has more than one of the<br />

access modifiers public, protected, and private.<br />

If no access modifier is specified for the constructor of a normal class, the<br />

constructor has default access. If no access modifier is specified for the constructor<br />

of an enum type, the constructor is private. It is a compile-time error if the<br />

constructor of an enum type (§8.9) is declared public or protected.<br />

If an annotation a on a constructor corresponds to an annotation type T, and T<br />

has a (meta-)annotation m that corresponds to annotation.Target, then m must<br />

have an element whose value is annotation.ElementType.CONSTRUCTOR, ora<br />

compile-time error occurs. Annotations are further discussed in §9.7.<br />

Unlike methods, a constructor cannot be abstract, static, final, native,<br />

strictfp,orsynchronized. A constructor is not inherited, so there is no need to<br />

declare it final and an abstract constructor could never be implemented. A<br />

constructor is always invoked with respect to an object, so it makes no sense for a<br />

constructor to be static. <strong>The</strong>re is no practical need for a constructor to be synchronized,<br />

because it would lock the object under construction, which is normally<br />

not made available to other threads until all constructors for the object have<br />

completed their work. <strong>The</strong> lack of native constructors is an arbitrary language<br />

design choice that makes it easy for an implementation of the <strong>Java</strong> virtual machine<br />

to verify that superclass constructors are always properly invoked during object<br />

creation.<br />

Note that a ConstructorModifier cannot be declared strictfp. This difference<br />

in the definitions for ConstructorModifier and MethodModifier (§8.4.3) is an<br />

intentional language design choice; it effectively ensures that a constructor is FPstrict<br />

(§15.4) if and only if its class is FP-strict.<br />

DRAFT<br />

241

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!