Puzzle 92: twisted pair wires

Source: Internet
Author: User

The following program uses an anonymous class to execute an unnatural action. What does it print?

public class Twisted {    private final String name;    Twisted(String name) {        this.name = name;    }    private String name() {        return name;    }    private void reproduce() {        new Twisted("reproduce") {            void printName() {                System.out.println(name());            }        }.printName();    }    public static void main(String[] args) {        new Twisted("main").reproduce();    }}

Based on a superficial analysis, the program cannot be compiled. The anonymous class in the reproduce method tries to call the private method name in the twisted class. A class cannot call the private method of another class, right? If you try to compile this program, you will find that it can be compiled successfully. In the top-level type, that is, the twisted class in this example, all local, internal, nested, and anonymous classes can access each other's members without restrictions [JLS 6.6.1]. This is a happy family.

After learning about this, you may want the program to print the reproduce because it calls the printname method on the new twisted ("reproduce") instance, this instance transmits the string "reproduce" to its super-class constructor to store it in its name domain. The printname method calls the name method. The name method returns the content of the Name field. However, if you run this program, you will find that it prints main. The question now is why does it do such a thing?

The reason behind this behavior is that private members are not inherited [JLS 8.2]. In this program, the name method is not inherited to the anonymous class in the reproduce method. Therefore, the call to the printname method in the anonymous class must be associated with the peripheral ("Main") instance rather than the current ("reproduce") instance. This is the minimum peripheral scope (enclosing scope) of the method containing the correct name (puzzles 71 and 79 ).

This program violates the advice in puzzle 90: The Anonymous class in "reproduce" is the internal class of the twisted class and extends it. This alone is enough to make the program difficult to read. Coupled with the complexity of calling a superclass private method, this program becomes purely lengthy nonsense. This puzzle can be used to emphasize the Lesson 6: if you cannot read the code to tell what the program will do, It is likely not to do what you want it to do. Strive for program clarity as much as possible.

Puzzle 92: twisted pair wires

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.