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="ba1fcf8667a5d5ae-e3883c68-4bbf4999-b2f296c2-a051b10af7bf1bdf46a22be1"><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="6fe1348153ef83df-a6a57e82-4b3b4662-81b2be8b-336bf4bba8d4c030fecc1cee"><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="75f6b6d1c0d01174-fa33c852-4a40486e-856786ba-213c84e894c45c3fd51055c4"><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="05f70f093cf48e37-ab476264-451c4c11-91f1aaa4-73f3ea40807eb2c72c95cc5b"><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>

...