Java方法访问标志解析:如何理解class文件中的access_flags

作者:袖梨 2026-06-01

在Java开发中,深入理解class文件结构对于查看方法访问标识至关重要。本文将系统解析12种方法访问标志的具体含义和验证方式。

背景

在日常开发过程中,开发者经常需要查看class文件内容。掌握class文件结构的基本知识能显著提升工作效率。由于该主题内容庞大,本文仅聚焦于解析class文件中方法的access flags。

要点

目前(JDK26)版本中,class文件字段的access flags共支持12种类型。每种类型都提供了对应的代码或命令验证方法。

img_6a1d15fb7904030.webp

正文

Java虚拟机规范4.6节详细介绍了class文件中method_info的结构,其开头部分如下所示。

img_6a1d15fb7904531.webp

u2 的含义

Java虚拟机规范第四章开头解释了u2的含义,表示2字节的无符号数。

img_6a1d15fb7904732.webp

方法的access flags

Java虚拟机规范4.6节提供了详细的访问标志表格,共包含12种情况。

Flag Name Value Interpretation
ACC_PUBLIC 0x0001 Declared public; may be accessed from outside its package.
ACC_PRIVATE 0x0002 Declared private; accessible only within the defining class.
ACC_PROTECTED 0x0004 Declared protected; may be accessed within subclasses.
ACC_STATIC 0x0008 Declared static.
ACC_FINAL 0x0010 Declared final; must not be overridden.
ACC_SYNCHRONIZED 0x0020 Declared synchronized; invocation is wrapped by a monitor use.
ACC_BRIDGE 0x0040 A bridge method, generated by the compiler.
ACC_VARARGS 0x0080 Declared with variable number of arguments.
ACC_NATIVE 0x0100 Declared native; implemented in another language.
ACC_ABSTRACT 0x0400 Declared abstract; no implementation is provided.
ACC_STRICT 0x0800 Declared strictfp in class files with major version 46-60.
ACC_SYNTHETIC 0x1000 Declared synthetic; not present in the source code.

前6种情况

验证代码保存为Simple.java,通过javac编译后运行可查看前6个访问标志。

import java.lang.reflect.Method;
public class Simple {
    private void method1() {}
    protected void method2() {}
    final void method3() {}
    synchronized void method4() {}
    public static void main(String[] args) {
        for (Method method : Simple.class.getDeclaredMethods()) {
            System.out.printf("method [%s] has access flags: %s%n",
                method.getName(), method.accessFlags());
        }
    }
}

运行结果如下:

method [main] has access flags: [PUBLIC, STATIC]
method [method1] has access flags: [PRIVATE]
method [method2] has access flags: [PROTECTED]
method [method3] has access flags: [FINAL]
method [method4] has access flags: [SYNCHRONIZED]

img_6a1d15fb7904933.webp

第7种情况:ACC_BRIDGE

通过实现Callable接口验证桥接方法。

import java.lang.reflect.Method;
import java.util.concurrent.Callable;
public class Case7 implements Callable {
    @Override
    public String call() { return ""; }
    public static void main(String[] args) {
        for (Method method : Case7.class.getDeclaredMethods()) {
            System.out.printf("method [%s] has access flags: %s%n",
                method.getName(), method.accessFlags());
        }
    }
}

运行结果如下:

method [main] has access flags: [PUBLIC, STATIC]
method [call] has access flags: [PUBLIC]
method [call] has access flags: [PUBLIC, BRIDGE, SYNTHETIC]

第8种情况:ACC_VARARGS

通过变长参数方法验证。

public class Case8 {
    int calcSum(int... nums) {
        int sum = 0;
        for (int num : nums) sum += num;
        return sum;
    }
    public static void main(String[] args) {
        for (Method method : Case8.class.getDeclaredMethods()) {
            System.out.printf("method [%s] has access flags: %s%n",
                method.getName(), method.accessFlags());
        }
    }
}

运行结果如下:

method [main] has access flags: [PUBLIC, STATIC]
method [calcSum] has access flags: [VARARGS]

第9种情况:ACC_NATIVE

通过Object类的native方法验证。

img_6a1d15fb7904b34.webp

第10种情况:ACC_ABSTRACT

通过Runnable接口的抽象方法验证。

第11种情况:ACC_STRICT

使用JDK8编译strictfp方法验证。

img_6a1d15fb7904d35.webp

第12种情况:ACC_SYNTHETIC

通过枚举类型验证合成方法。

参考资料

Java虚拟机规范中的相关内容:

  1. 第四章 class文件格式
  2. 4.6节 方法

其他

图表绘制方法

使用Python3 matplotlib库绘制访问标志示意图。

通过本文的系统解析,我们全面掌握了Java class文件中12种方法访问标志的含义及验证方法,为深入理解字节码结构奠定了坚实基础。

相关文章

精彩推荐