Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Minor changes based on JP-CERT comments.

Do not reuse the names of publicly-visible identifiers, public utility classes, interfaces, and or packages in the Java Standard Library.

Wiki Markup
IfWhen a developer uses an identifier that has the same name as a public class, such as {{Vector}}, a subsequent maintainer might not be aware that this identifier does not actually refer to {{java.util.Vector}}, and might unintentionally use the custom {{Vector}} instead of the original {{java.util.Vector}} class. The custom type {{Vector}} can [shadow|BB. Definitions#shadow] a class name from {{java.util.Vector}}, as specified by [§6.3.2|http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.3.2] of the _Java Language Specification_ \[[JLS 2005|AA. Bibliography#JLS 05]\]. This can result in unexpected program behavior.

...

This compliant solution uses a different name for the class, preventing any potential shadowing of the class from the Java Standard Library.

Code Block
bgColor#ccccff
class MyVector {
  //other code
}

Wiki Markup
When the developer and organization control the original shadowed class, it may be preferable to change the design strategy of the original in accordance with Bloch's _Effective Java_ \[[Bloch 2008|AA. Bibliography#Bloch 08]\] "Item 16: Prefer interfaces to abstract classes." Changing the original class into an interface would permit class {{MyVector}} to declare that it implements the hypothetical {{Vector}} interface. This would permit client code that intended to use {{MyVector}} to remain compatible with code that uses the original implementation of {{Vector}}.

Risk Assessment

Name Public identifier reuse decreases the readability and maintainability of code.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="667bcfdcb26fa361-2d1b08e7-48a2468f-ab23a092-6eadfda52e393d7ee11b9793"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[§6.3.2, "Obscured Declarations"

http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.3.2]

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

 

§6.3.1, "Shadowing Declarations"

 

§7.5.2, "Type-Import-on-Demand Declaration"

 

§14.4.3, "Shadowing of Names by Local Variables"

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e957b8692331cd58-7c071591-427f4abf-9b3991b1-d2f7b0569caba49060dd03be"><ac:plain-text-body><![CDATA[

[[FindBugs 2008

AA. Bibliography#FindBugs 08]]

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f4c38bd70e02d37f-1de9526e-469f48cc-9b41b778-222c9b7ae21dba464d0ae2b3"><ac:plain-text-body><![CDATA[

[[Bloch 2005

AA. Bibliography#Bloch 05]]

Puzzle 67: All Strung Out

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="00c2d8e83e4e5e7f-eb4a66a5-43184e78-9ac5b312-0c7b4211a966975878d6a4fe"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch 08]]

Item 16: Prefer interfaces to abstract classes

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

...