Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Unlike C++, the Java programming language does not specify altered rules for method dispatch during the creation of a new class instance. If methods are invoked that are overridden in subclasses in the object being initialized, then these overriding methods are used, even before the new object is completely initialized.

Consequently, invocation Invocation of an overridable method during object construction may result in the use of uninitialized data, leading to runtime exceptions or to unanticipated outcomes. Calling overridable methods from constructors can also leak the this reference before object construction is complete, potentially exposing uninitialized or inconsistent data to other threads. See rule TSM01-J. Do not let the (this) reference escape during object construction for additional information. As a result, constructors may must not invoke only methods that are not final or private; all other method invocations are forbidden.

Noncompliant Code Example

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="922246052c33bdb3-31fecf7f-440045c1-aeb7bab4-ed11d19270b63b2b3a594129"><ac:plain-text-body><![CDATA[

[ISO/IEC TR 24772:2010

http://www.aitcnet.org/isai/]

"Inheritance [RIP]"

]]></ac:plain-text-body></ac:structured-macro>

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="864d7c41095f877e-6116ccdc-4d8a4bce-a259851b-9498e3330cab20be3310f120"><ac:plain-text-body><![CDATA[

[[ESA 2005

AA. Bibliography#ESA 05]]

Rule 62: Do not call non-final methods from within a constructor

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f81964ce0fca5e1a-1d069dde-40594cc4-97e79c9b-8cd0b2ef616893c0711272e3"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[Chapter 8, Classes

http://java.sun.com/docs/books/jls/third_edition/html/classes.html], 12.5 "Creation of New Class Instances"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8e236487c4b2a339-deea539e-46c84532-9ceabe10-6c0e0c7f2b6286f4d1a01b3a"><ac:plain-text-body><![CDATA[

[[Rogue 2000

AA. Bibliography#Rogue 00]] Rule 81:

Do not call non-final methods from within a constructor

]]></ac:plain-text-body></ac:structured-macro>

Secure Coding Guidelines for the Java Programming Language, Version 3.0

Guideline 4-4 Prevent constructors from calling methods that can be overridden

...