Each field (class variable and instance variable) declared in a class or interface is described by a field_info table in the class file. The format of the field_info table is shown in Table 6-20.
在类或者接口中声明的每一个字段(类变量或者实例变量)都由class文件中的一个名为field_info的可变长度的表进行描述。在一个class文件中,不会存在两个具有相同名字和描述符的字段。需要注意的是尽管在Java程序设计语言中不会有两个相同名字的字段存在于同一个类或者接口中,但一个class文件中的两个字段可以拥有同一个名字——只要它们的描述符不同。换句话说,尽管在程序设计语言中无法在同一个类或者接口中定义两个具有同样名字和不同类别的字段,但是两个这样的字段却可以同时合法地出现在一个Javaclass文件中。表6-20中列出了field_info表的格式。
Table 6-20. Format of a field_info table
Type | Name | Count |
u2 | access_flags | 1 |
u2 | name_index | 1 |
u2 | descriptor_index | 1 |
u2 | attributes_count | 1 |
attribute_info | attributes | attributes_count |
Field_info表中各项如下所示:
access_flags
The modifiers used in declaring the field are placed into the fieldís access_flags item. Table 6-21 shows the bits used by each flag.
声明字段时使用的修饰符存放在字段的access_flags项中。表6-21列出了各个标志所使用的位。
Table 6-21. Flags in the access_flags item of field_info tables
Flag Name | Value | Meaning if Set | Set By |
ACC_PUBLIC | 0x0001 | Field is public | Classes and interfaces |
ACC_PRIVATE | 0x0002 | Field is private | Classes only |
ACC_PROTECTED | 0x0004 | Field is protected | Classes only |
ACC_STATIC | 0x0008 | Field is static | Classes and interfaces |
ACC_FINAL | 0x0010 | Field is final | Classes and interfaces |
ACC_VOLATILE | 0x0040 | Field is volatile | Classes only |
ACC_TRANSIENT | 0x0080 | Field is transient | Classes only |
For fields declared in a class (not an interface), at most one of ACC_PUBLIC, ACC_PRIVATE, and ACC_PROTECTED may be set. ACC_FINAL and ACC_VOLATILE must not both be set. All fields declared in interfaces must have the ACC_PUBLIC, ACC_STATIC, and ACC_FINAL flags set.
类(不包括接口)中声明的字段,只能拥有ACC_PUBLIC,ACC_PRIVATE,ACC_PROTECTED这三个标志中的一个。ACC_FINAL和ACC_VOLATILE不能同时设置。所有接口中声明的字段必须有且只能有ACC_PUBLIC,ACC_STATIC和ACC_FINAL这三种标志。
All unused bits in access_flags must be set to zero and ignored by Java Virtual Machine implementations.
Access_flags中没有用到的位都被设为0,Java虚拟机实现将忽略它们。
name_index
The name_index gives the index of a CONSTANT_Utf8_info entry that gives the simple (not fully qualified) name of the field.
Name_index项提供了给出字段简单名称(不是全限定名)的CONSTANT_Utf8_info入口的索引。在class文件中的每一个字段的名称都必须符合Java程序设计语言中对名称的有效规定。
descriptor_index
The descriptor_index gives the index of a CONSTANT_Utf8_info entry that gives the descriptor of the field.
Descriptor_index提供了给出字段描述符的CONSTANT_Utf8_info入口的索引。
attributes_count and attributes
The attributes item is a list of attribute_info tables. The attributes_count indicates the number of attribute_info tables in the list. Two kinds of attributes defined by the Java Virtual Machine specification that may appear in this item are ConstantValue and Synthetic. These two attributes are described in detail later in this chapter.
Attributes项是由多个attribute_info表组成的列表。Attributes_count指出列表中attribute_info表的数量。一个字段在其列表中可以有任意数量的属性。由Java虚拟机规范定义的三种可能会出现在此项中的属性是:ConstantValue,Deprecated和Synthetic。Java虚拟机唯一需要识别的属性是ConstantValue属性。虚拟机实现必须忽略任何无法识别的属性。