Home  >  Article  >  Java  >  The relationship between Java function access modifiers and field access

The relationship between Java function access modifiers and field access

王林
王林Original
2024-04-25 18:09:021011browse

A Java function's access modifier affects its access to fields in the class: public functions can access all fields, regardless of field access permissions. Protected functions can only access fields with protected or public access. The default function can only access fields with default or public access. Private functions can only access fields with private access.

Java 函数的访问权限修饰符之与字段访问的联系

The relationship between the access permission modifiers of Java functions and field access

The access permission modifiers of Java functions can restrict their access to fields in the class access permission. Understanding this connection is critical to maintaining the security and maintainability of your code.

Access permission modifiers

There are four function access permission modifiers in Java:

  • public: Allow all classes to access functions .
  • protected: Allows classes and subclasses belonging to the same package to access functions.
  • default (no modifier): Only classes belonging to the same package are allowed to access the function.
  • private: Allow only the class itself to access functions.

Contact of field access

The access modifier of a function affects its access to fields in the class:

  • If the function is public, you can access all fields, regardless of the field's access permissions.
  • If the function is protected, only fields with protected or public access rights can be accessed.
  • If the function is default, only fields with default or public access rights can be accessed.
  • If the function is private, only fields with private access permissions can be accessed.

Practical case

Consider the following class:

public class MyClass {
    private int privateField;
    protected int protectedField;
    int defaultField;
    public int publicField;

    public void publicMethod() {
        // 可以访问所有字段
        System.out.println(privateField);
        System.out.println(protectedField);
        System.out.println(defaultField);
        System.out.println(publicField);
    }

    protected void protectedMethod() {
        // 可以访问 protected 和 public 字段
        System.out.println(protectedField);
        System.out.println(publicField);
    }

    void defaultMethod() {
        // 可以访问 default 和 public 字段
        System.out.println(defaultField);
        System.out.println(publicField);
    }

    private void privateMethod() {
        // 只能访问 private 字段
        System.out.println(privateField);
    }
}

In this case:

  • publicMethod() All fields can be accessed because it is a public method.
  • protectedMethod() has access to protectedField and publicField because it is a protected method.
  • defaultMethod() has access to defaultField and publicField because it is a method visible within the package by default.
  • privateMethod() can only access privateField because it is a private method.

The above is the detailed content of The relationship between Java function access modifiers and field access. 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