Glossary of Java FAQ names reuse

Source: Internet
Author: User
Tags naming convention

Overwrite (override)

An instance method can overwrite (override) all instance methods that have the same signature that are accessible in its superclass [JLS 8.4.8.1], enabling dynamic dispatch (dynamically dispatch); in other words, the VM will Select the override method to invoke [JLS 15.12.4.4] Based on the instance's run-time type . Overwrite is the basis of object-oriented programming technology and is the only form of name reuse that is not universally discouraged:

<span style= "FONT-SIZE:18PX;" >class Base {public     void F () {}}class Derived extends Base {public     void F () {}//Overrides BASE.F ()}</s Pan>


Hidden (hide)

A domain, static method, or member type can hide (hide) all fields, static methods, or member types that have the same name (the same method signature on the method) that are accessible in its superclass. Hiding a member will prevent it from being inherited [JLS 8.3, 8.4.8.2, 8.5]:

<span style= "FONT-SIZE:18PX;" >class Base {public     static void F () {}} class Derived extends Base {     private static void F () {}//Hides Bas E.F ()}</span>


overloading (overload)

Methods in a class can overload (overload) Another method, as long as they have the same name and different signatures . The overloaded method specified by the call is the [JLS 8.4.9, 15.12.2] selected at compile time :

<span style= "FONT-SIZE:18PX;" >class circuitbreaker {public     void F (int. i)     {}//int overloading public     void F (string s) {}//String o Verloading}</span>

Masking (Shadow)

A variable, method, or type can individually obscure (shadow) all variables, methods, or types that have the same name within a closed text range . If an entity is obscured, then you cannot refer to it with its simple name, and sometimes you cannot refer to it at all, depending on the entity [JLS 6.3.1]:

<span style= "FONT-SIZE:18PX;" >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     }}</span>


Although shadowing is usually discouraged, there is a common idiom that does cover it. Constructors often reuse a domain name from its own class as a parameter to pass the value of the named field. This idiom is not without risk, but most Java programmers think the benefits of this style outweigh its risks:

<span style= "FONT-SIZE:18PX;" >class Belt {     private final int size;     public Belt (int size) {//Parameter shadows belt.size          this.size = size;     }} </span>


masking (obscure)

A variable can obscure a type that has the same name, as long as they are all in the same range: if the name is used in a scope where the variable and type are permitted, then it is referenced to the variable. Similarly, a variable or a type can obscure a package. Masking is the only form of two name reuse in different namespaces that include: variables, packages, methods, or types. If a type or a package is obscured, then you cannot refer to it by its simple name, except in a context where the syntax allows only one name to appear in its namespace. Following a naming convention can greatly eliminate the possibility of masking [JLS 6.3.2, 6.5]:

<span style= "FONT-SIZE:18PX;" >public class Obscure {     static String System;  Obscures type Java.lang.System public     static void Main (string[] args) {          //Next line won ' t Compile:system ref ERs to static field          System.out.println ("Hello, Obscure world!");}     } </span>


Glossary of Java FAQ names reuse

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.