Home >Java >javaTutorial >The relationship between Java function access modifiers and anonymous inner classes

The relationship between Java function access modifiers and anonymous inner classes

王林
王林Original
2024-04-25 15:33:01679browse

The access modifier of a Java function determines the visibility scope of the function, including public, protected, default and private. As a class inside the outer class, the anonymous inner class can access all non-private members of the outer class, so the access rights of its functions are related to the function access rights of the outer class.

Java 函数的访问权限修饰符之与匿名内部类的关系

The relationship between the access modifiers of Java functions and anonymous inner classes

The access modifiers of Java functions determine the function From which locations it can be accessed. These modifiers include:

  • public: Accessible from anywhere
  • protected: Accessible from the same package or subpackage
  • default (package): Accessible only from the same package
  • private: Accessible only within the class in which the function is defined

Anonymous inner class is a class defined within a class in Java without a specified name. They are typically used to create single-use objects, such as implementing event handlers or comparators.

Anonymous inner classes can access all non-private members of the outer class. Therefore, the access rights of functions in an anonymous inner class are related to the access rights of functions in its outer class. For example:

public class OuterClass {

    private int privateField = 1;
    protected int protectedField = 2;
    int defaultField = 3;
    public int publicField = 4;

    public void someMethod() {
        new Runnable() {

            @Override
            public void run() {
                // 内部类可以访问所有非私有成员变量
                System.out.println(protectedField);
                System.out.println(defaultField);
                System.out.println(publicField);
            }
        }.run();
    }
}

In the above code, the functions in the anonymous inner class can access protectedField, defaultField and publicField because they are not private.

Practical case:

Suppose we have an EventProcessor interface, which defines a process() method. We want to create an anonymous inner class to implement the process() method, which can access the data in the outer class.

public class Main {

    private String data = "Hello";

    public static void main(String[] args) {
        EventProcessor processor = new EventProcessor() {

            @Override
            public void process() {
                // 匿名内部类可以访问外部类中的 data 成员变量
                System.out.println(data);
            }
        };

        processor.process();
    }
}

In this case, as long as the data member variable is not private, functions in the anonymous inner class can access it from the same package or subpackage.

The above is the detailed content of The relationship between Java function access modifiers and anonymous inner classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn