Java class file types

Source: Internet
Author: User

The Java bytecode we see is encapsulated in a class file with the extension. class. In this tutorial, let's look at the internal structure of the class file.

How the data is written to the class file and the format of the class file, let's start with a schematic look at the Java class file.

Java class file structure:

The depicted Java class files are divided into sections, including Magic Code (Magic), version, Chang (constant pool), access identifier (access flags), (This) class, (Super) class, interface ( interfaces), Domain (fields), Method (methods), and properties (attributes).

First, the length of the Java class is unknown until it is loaded, because the class contains sections of variable length, such as the pool of constants, methods, properties, and so on. However, the opening bytes of these sections are the size or length information of the segment, so that the JVM knows the size of these variable-length extents before actually loading them. The data written to the class file is stored in a compact single byte, which helps reduce the size of the class file. The order of the different sections in the Java class file is strictly defined so that the JVM can load different extents in its order. Let's look at each component in the class file in detail.

(Translator Note: These 10 sections M agic, V ersion, C onstant pool, A ccess flags, T His, S Uper, I nterfaces, F ields, Methods, a ttributes you can easily memorize their order in English with a single sentence:M  Y V ery C ute A nimal T urns S avage I n Full M oon A reas. My very lovely little animal became crazy in the place full of months. )

Magic Code (Magic) Section

Magic code is used to identify file formats in order to differentiate them from other formats. The first four bytes of a class file, the Magic code, 0xCAFEBABE. (Translator Note: 16 binary encoding is the English word Cafe babe[Coffee Baby], to facilitate the memory)


Version number section

The four bytes that follow contain the major and minor version numbers of the class file. A version number allows the JVM to verify and determine if the class file is compatible. If the version number is greater than the version number that the JVM can load, the class file cannot be loaded.


Chang (constant Pool) section

Constants defined by a class or interface are stored in a constant pool, including the class name, variable name, interface name, method name and signature, constant value, string literal, and so on. Constants are stored in a constant pool as a variable-length array of elements. The constant array size is known, so the JVM knows how many constants are in the constant pool when it loads the class file.



, the part that is represented in green is the size of the array. The first byte of each array element is used to mark the type of the constant. The part that is represented in Orange is the tag byte. The virtual machine JVM reads this byte to identify the type of the constant. If a byte token indicates that this is a string, then the JVM knows that the following 2 bytes represent the length of the string and the subsequent part is the string itself.

(Translator Note: The original description of the constant pool is missing a key element: the value of the constant pool count is the total number of elements in the constant pool +1, and the element's subscript starts from 1.) If the count is 3, then the count is only two elements, and the subscript is 1, 2. One more element of the original map, correct here)


Access Flags Section

The Access Identity section is just after the constant pool segment. The Access Identity section has only 2 bytes of data to identify whether the file defines a class or an interface, which is public,abstract or final.

section of this class

The This class is also a 2-byte data, whose value is a subscript for a constant array in a constant pool.



, the value of the This class 0x0007 points to a constant in the constant pool. The corresponding entry in that constant pool consists of two parts, the first part being a byte tag, representing the type of the constant pool entry, a class or an interface. The type of the constant entry that is identified by the orange section. followed by the two-byte data, the value of the data is another constant in the constant pool value. In the figure, the two-byte value is 0x0004, which points to the string constant of the interface or class name in the constant pool.

Super class Section

The 2 bytes after this class segment are the Super class segment. Structurally similar to this class, this two-byte value also points to a subscript in the constant pool, and another constant value that follows the constant value is the string that points to the parent class name.

Interface (interfaces) segment

All interfaces implemented by this class or interface are defined in the interface section of the class file. The first 2 bytes of the interface section provide information about the number of interfaces implemented. followed by an array containing the constant subscript of the string that points to the interface name implemented in the constant pool.

field (fields) Section

A domain refers to a variable defined in an instance or interface of this class. The domain of each class file contains only the variables defined within the class or interface, not the variables inherited from the parent or parent interfaces. The first 2 bytes in the domain section are a count, which records the total number of variables defined in the domain. After the count is followed by an array, each element in the array is defined as a domain variable, and the variable definition is made up of structures of different lengths. Some parts of variables are defined as stored in structs, and some parts, such as variable names, are stored in a constant pool.

Method (Methods) Section

The method section holds all the methods defined in this class or interface, and, like a domain, does not include methods inherited from the parent class. The first 2 bytes in a section are also recorded in the same way as the number of methods defined by this class or interface. The rest is also an array, and each element in the array is the structure of a method. Each method structure contains some of the information in this method, such as parameter list, return type, the number of stack words required by the method local variables, stack words required by the method stack, exception table and byte code sequence, etc.

attribute (attributes) segment

The attribute section contains some property information for this class file, such as one of the properties of the source code attribute, which is used to represent the source name of the class bytecode file. The first two bytes of the attribute section are the number of attributes in the segment, followed by the property itself. The JVM ignores any attributes it does not recognize.

Java class file types

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.