It is necessary to keep in mind that the glossary for name reuse from Java Obfuscation

Source: Internet
Author: User

Override)

An instance method can overwrite (override) All instance methods with the same signature that can be accessed in its superclasses [JLS 8.4.8.1], thus enabling dynamic dispatch (Dynamic dispatch ); in other words, the VM selects the override method to be called Based on the instance runtime type [JLS 15.12.4.4]. Overwriting is the basis of object-oriented programming technology, and is the only form of name reuse that is not generally discouraged:

class Base {
     public void f() { }
}
 
class Derived extends Base {
     public void f() { } // overrides Base.f()
}
Hide)

A domain, static method, or member type can respectively hide (hide) the same name that can be accessed in its superclass (the same method signature is used for methods) all domains, static methods, or member types. Hiding a member will prevent it from being inherited [JLS 8.3, 8.4.8.2, 8.5]:

class Base {
     public static void f() { }
}
 
class Derived extends Base {
     private static void f() { } // hides Base.f()
}
Overload)

Methods In a class can overload another method, as long as they have the same name and different signatures. The overload method specified by the call is selected during the compilation period [JLS 8.4.9, 15.12.2]:

class CircuitBreaker {
     public void f(int i)     { } // int overloading
     public void f(String s) { } // String overloading
}
Shadow)

A variable, method, or type can respectively mask all variables, methods, or types with the same name within a closed text range (Shadow. If an object is masked, its simple name cannot be referenced. Based on the entity, sometimes you cannot reference it [JLS 6.3.1]:

class WhoKnows {
     static String sentence = "I don't know.";
     public static woid main(String[ ] args) {
          String sentence = “I know!”;   // shadows static field
          System.out.println(sentence);  // prints local variable
     }
}

Although masking is usually discouraged, there is a common practice that does involve masking. Constructors often reuse a domain name from its class as a parameter to pass the value of this namefield. This practice is not without risks, but most Java Programmers think that the benefits brought by this style exceed the risks:

class Belt {
     private final int size;
     public Belt(int size) { // Parameter shadows Belt.size
          this.size = size;
     }
}
Obscure)

A variable can hide a type with the same name, as long as they are all in the same range: if the name is used in the range where both variables and types are permitted, then it will reference the variable. Similarly, a variable or type can mask a package. Masking is the only form of name reuse in which two names are located in different namespaces. These namespaces include variables, packages, methods, or types. If a type or package is hidden, you cannot reference it with its simple name, unless it is in such a context, that is, the syntax allows only one name in its namespace. Follow naming conventions to greatly eliminate the possibility of masking [JLS 6.3.2, 6.5]:

public class Obscure {
     static String System;  // Obscures type java.lang.System
     public static void main(String[ ] args) {
          // Next line won't compile: System refers to static field
          System.out.println(“hello, obscure world!”);
     }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.