Java Virtual Machine (JVM) Internals, Part 2 — Class file format

Prateek Saini
Javarevisited
Published in
5 min readJul 7, 2019

In this series of articles I’ll be discussing about how Java Virtual Machine works. In part 1 we looked at ClassLoader sub-system of Java Virtual Machine. In this article we are going to talk about Class file format.

As we already know, all source code written in Java programming language is first compiled into bytecodes (instructions for Java Virtual Machine) by using javac compiler provided in Java Development Kit. Bytecodes are saved in a binary file format called class file format. These class files (bytecodes) are then loaded dynamically (only when required) into memory by Class loader component of Java Virtual Machine. In simple interpreter mode, Java execution engine executes these bytecodes one by one on host machine CPU.

Java source code compilation

Every file with .java extension is compiled into at least one .class file. There is one .class file for each class, interface and module defined in source code. This applies to nested classes or interfaces too.

Note : For simplicity files with .class extension is called class file here.

Let’s write a simple program

Running javac on this file produces following files.

ClassOne$StaticNestedClass.class
ClassOne.class
ClassTwo.class
InterfaceOne.class

As you can see one class file is created for each class, interface defined in the source file.

What’s inside Class file ?

Class file is a binary file format. Information is generally written to the class file with no space or padding between consecutive pieces of information, everything is aligned on byte boundaries. All 16-bit and 32-bit quantities are constructed by reading two and four consecutive 8-bit bytes, respectively.

Class file contains the following information

Magic number : The first four bytes of every class file are always 0xCAFEBABE. These four bytes identifies the class file format from others.

Major and Minor version : The second four bytes of the class file contain the major and minor version numbers. Together, a major and a minor version number determine the version of the class file format. If a class file has major version number M and minor version number m, we denote the version of its class file format as M.m.

Every JVM has a maximum version it can load, and JVMs will reject class files with later versions. For example Java 11 supports major versions from 45 to 55 and Java 12 supports major version 45..56

Constant Pool : A table of structures(heterogeneous) representing string constants, class and interface names, field names, method names and other constants that are referred to within the ClassFile structure and its substructures. Each element of this constant pool starts with a one-byte tag specifying the type of constant at that position in the table. Depending on the type of constant next bytes can be the constant-value or reference to another element in the pool.

Access flags : List of flags which tells whether this class or interface has public or private access, whether this class is final or allow extensions. Various flags such as ACC_PUBLIC, ACC_FINAL, ACC_INTERFACE, ACC_ENUM etc are defined in JVM Specification document.

This class : Refers to entry in constant pool.

Super class : Refers to entry in constant pool.

Interfaces : Count of number of interfaces implemented by this class.

Field Count : Count of the number of fields in this class or interface.

Fields : Following the field count is a table of variable-length structures, one for each field describing type of field and name (reference to constant pool entry)

Method Count : Count of the number of methods in the class or interface.This count includes only those methods that are explicitly defined by this class, not any methods that may be inherited from superclasses.

Methods : Following the method count are the methods themselves.The structure for each method contains several pieces of information about the method including the method descriptor (its return type and argument list), the number of words required for the method’s local variables, the maximum number of stack words required for the method’s operand stack, a table of exceptions caught by the method, the bytecode sequence, and a line number table.

Attribute Count : Count of the number of attributes in this class or interface or module.,

Attributes : Following the attribute count is table or variable-length structures describing each attribute. For example, one attribute is the source code attribute; it reveals the name of the source file from which this class file was compiled.

Although class file format is not readable directly but JDK provides a tool called javap which dissemble class file and outputs it’s content in readable format.

Let’s write a simple Java program as mentioned below

Let’s compile this program using javac command which will produce HelloWorld.class file and then use javap tool to dissemble this HelloWorld.class file. Using javap with -v (verbose) on HelloWorld.class produces the following output

Here you can see that this class is publicly accessible , it’s constant pool has 37 entries, has one attribute (SourceFile at the bottom), implements two interfaces (Serializable, Cloneable), has zero fields, and 2 methods.

You might be wondering that there is only one static main method in source code but class file says there are two methods. Well, remember default constructor in Java programming language, it’s a no-arg constructor added by javac compiler whose bytecodes are also visible in output. Constructors are treated as methods.

You can read more about javap tool here.

Tip : you can use javap tool to see how lambdas are different than anonymous inner classes.

In next part of this series I will talk about memory layout of running JVM instance.

Other Useful Resources for learning Java you may like
Top 5 Courses to learn JVM internals
10 Free Courses to Learn Java from Scratch
10 Books to Learn Java in Depth
10 Tools Every Java Developer Should Know
10 Reasons to Learn Java Programming languages
10 Frameworks Java and Web Developer should learn in 2019
10 Tips to become a better Java Developer in 2019
Top 5 Java Frameworks to Learn in 2019
10 Testing Libraries Every Java Developer Should Know

--

--