OverloadingIn particular, shadowing (the forementioned constructor/setter idiom) and obscuring (method name reused for return variable) are two of my own practices that I can now vernaculate.
Methods in a class overload one another if they have the same name and different signatures. The overloaded method designated by an invocation is selected at compile time [JLS 8.4.9, 15.12.2].
A variable, method, or type shadows all variables, methods, or types, respectively, with the same name in a textually enclosing scope. If an entity is shadowed, you cannot refer to it by its simple name; depending on the entity, you cannot refer to it at all [JLS 6.3.1].
Although shadowing is generally discouraged, one common idiom does involve shadowing. Constructors often reuse a field name from their class as a parameter name to pass the value of the named field. This idiom is not without risk, but most Java programmers have decided that the stylistic benefits outweigh the risks.
A variable obscures a type with the same name if both are in scope: If the name is used where variables and types are permitted, it refers to the variable. Similarly, a variable or a type can obscure a package. Obscuring is the only kind of name reuse where the two names are in different namespaces: variables, packages, methods, or types. If a type or a package is obscured, you cannot refer to it by its simple name except in a context where the syntax allows only a name from its namespace. Adhering to the naming conventions largely eliminates obscuring [JLS 6.3.2, 6.5].
An instance method overrides all accessible instance methods with the same signature in superclasses [JLS 220.127.116.11], enabling dynamic dispatch; in other words, the VM chooses which overriding to invoke based on an instance's run-time type [JLS 18.104.22.168]. Overriding is fundamental to object-oriented programming and is the only form of name reuse that is not generally discouraged.
A field, static method, or member type hides all accessible fields, static methods, or member types, respectively, with the same name (or, for methods, signature) in supertypes. Hiding a member prevents it from being inherited [JLS 8.3, 22.214.171.124, 8.5].
Sunday, February 7, 2010
This is a one-page summary that is somewhat hidden in Java Puzzlers. I feel that knowing the proper names for these concepts is important enough that it's worth quoting for future reference. I've taken the liberties to reorder these as I see fit: