How the Field table collection--field field in the class file is organized in the Java Virtual machine schematic diagram 1.4 class file

Source: Internet
Author: User
Tags field table modifiers

0. Preface

Understanding the JVM virtual machine principle is a necessary step for every Java programmer to practice. But because there are a lot of things in the JVM virtual machine to tell the relatively broad, in the current exposure to the JVM virtual machine principle of the tutorial or blog, the vast majority of the text-based description, it is difficult to give people a visual perception, after reading the feeling is still confused.

Feeling the above, I intend to learn from the process of the JVM virtual machine I learned, combined with their own understanding, summed up into the "Java Virtual machine schematic diagram" This series, in the form of illustrations, the abstract JVM virtual machine knowledge embodied, You want to be able to help Java programmers who want to understand the fundamentals of Java virtual machines.


After reading this article, you will learn:

1. The field field defined in the class is how it is organized in the class file.

2. How different data types are represented in the class file

3,static FINAThe initialization assignment problem of field fields of type L

1. Overview

a collection of field tables refers to a collection of several field tables (field_info) . For several fields defined in a class, after the JVM is compiled into a class file, the corresponding field information is organized into a structure called a collection of field tables, and the Field table collection is a class array structure, as shown in:

Note: The field described here refers to a static or non-static variable defined in a class, not a variable defined within a method in a class. Please note the difference.

For example, if 5 fields are defined in a class, theJVM generates 5 Field table (field_info) information when compiling this class, and then sets the value of the field counter in the Field table collection to 5. Place 5 Field table information after the field counter in turn.

2. Location of the Field table collection in the class file

The Field table collection is immediately following the interface index collection structure of the class file, as shown in:


3. What information should be included in one field in Java? ------Field Table Field_info structure definition

for the above field representation, theJVM Virtual Machine specification Specifies the field_info struct to describe the field, which represents the following information:

Here I will explain the constituent elements of Field_info : The Access flag (access_flags), the name Index (name_index), the description index (descriptor_ Index), property sheet collection

4. Access Flags for field fields

as shown in the definition of the field_info struct , the Field field's access flag (access_flags) occupies two bytes, The information it can express is as follows:

Example: If we have a field field defined in a class:private static String str;, then on the access flag, the first bit of acc_private and the acc_static flag bit should be 1. The access flag information for the field domain str should be as follows:


As shown, the value of the access flag for thestr field is 0x000A, which consists of two modifiers, acc_private , and acc_static .

Based on the given access flag (access_flags), we can use the following operation to get the modifiers for this field:

The value of the access flag for the STR field listed above is 000A, so the eigenvalues of the above markers are &, and the result is 1 only acc_private and acc_static , so the field's markers are only acc_private and acc_static.



5. field data type representation and field name representation

The class file represents the data type as shown in the following:



field name, we define a field that is shaped like the private static String str , where "str" is the name of the field.

The class file stores the field name and the data type representation of the fields field as a string in a constant pool. In the field_info struct, immediately following the access flag is the field name index and the field Descriptor index, which occupy two bytes, which are stored internally to the index of a constant pool item in the constant pool, the string stored in the corresponding constant pool item, Represents the name and field descriptor of the field, respectively.


6. Initialization of a static field field-----A collection of property sheets

In the process of defining field fields, it is sometimes natural to assign values directly to field fields, as follows:

public static final int max=100;public  int count=0;
for virtual machines, the time to assign values for the two field fields above is different:

    •        for non-static (that is, no static decorated) field field assignment will appear in the instance construction method <init> () In
    •        for static < Strong>field field, with two options: 1, in the static construction method Span style= "font-family:comic Sans MS" ><cinit> () , 2, using constantvalue property is assigned
the Sun Javac compiler's initialization assignment policy for static field fields

The current selection of the Sun Javac compiler is: if you useFinalAndStaticAlso modifies aFieldfield, and this field is the base type orStringType, the compiler compiles this field in the correspondingField_infoStructure, add aConstantvalueType of struct, use this when assigning a valueConstantvalueTo assign the value;FieldField has not beenFinalmodifier, or not a basic type orStringType, then the method is constructed in the class<cinit> ()The value of the assignment.

For the above public static final init max=100; The Javac compiler constructs a field_info structure in this field, and adds a constantvalue type of attribute table in addition to the access flag, name index, and description Fusso.


7. Example Analysis:

Define the following simple class , and then analyze str field by viewing the contents of the simple.class file and combining the contents of the constant pool generated by javap-v . Structure of the field:

Package Com.louis.jvm;public class Simple {private  transient static final String str = "The is a Test";}


Note:

1. The value in the field counter is 0x0001, which means that the class defines a field
2. the access flag for the field is 0x009a, and the binary is 00000000 10011010, that is, 9, 12, 13, the mark bit 1, The markers for this field are:acc_transient, Acc_final, acc_static, acc_private;

3. The value in the name index is 0x0005, which points to item 5 in the constant pool, which is "str", indicating that the field name is str;

4. The value in the description index is 0x0006, which points to item 6 in the constant pool, which is "ljava/lang/string;", indicating that the The Data type for field fields is java.lang.String type;

5. The value in the attribute table counter is 0x0001, indicating that Field_info also has an attribute table;

6. The value in the attribute table name index is 0x0007, which points to item 7 in the constant pool, "constantvalue", indicating that the name of this property sheet is Constantvalue, that is, the type of the attribute table is constantvalue type;

7. The value in the property length is 0x0002, because this property sheet is of type constantvalue and its value is fixed to 2;

8. The value in the constant value index is 0x0008, which points to item 8 in the constant pool, which is an item of type Constant_string_info , indicating "This isa test "constant. When this field is assigned, this constant is used to assign a value to field .







author's words

This article is one of the "schematic diagrams of Java Virtual machines" series, and if you are interested, please follow other articles in this series ~

Think this article is good, handy point a praise Oh ~ ~ Your encouragement, is I continue to share the powerful power of knowledge!


-------------- --------------------------------------------------------------------------------------------------------------- ------------

This article originates from http://blog.csdn.net/luanlouis/, if need reprint, please indicate source, thank you!



How the Field table collection--field field in the class file is organized in the Java Virtual machine schematic diagram 1.4 class file

Related Article

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.