Class File Structure

Class File Structure
ClassFile {
    u4               magic_number;
    u2               minor_version;
    u2               major_version;
    u2               constant_pool_count;
    cp_info       constant_pool[constant_pool_count-1];
    u2               access_flags;
    u2               this_class;
    u2               super_class;
    u2               interfaces_count;
    u2               interfaces[interfaces_count];
    u2               fields_count;
    field_info   fields[fields_count];
    u2               methods_count;
    method_info  methods[methods_count];
    u2               attributes_count;
    attribute_info attributes[attributes_count];
}
magic_number
  • The first 4 bytes of class file is magic number.
  • This is a predefined value to identify the Java class file.
  • This value should be 0xCAFEBABE.
  • JVM will use this value to identify whether the class file is valid or not and also to know whether the class file is generated by valid compiler or not.
  • Whenever we are executing a Java class if JVM unable to find magic_number then we will get runtime error saying java.lang.ClassFormatError : Incompatible magic number
major and minor versions
  • major and minor versions represent class file version
  • JVM will use these versions to identify which version of compiler generates the current .class file
  • 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.
  • Major and minor versions both are allocates 2 bytes
  • The possible values are
major  minor   Java platform version 
 45       3           1.0
 45       3           1.1
 46       0           1.2
 47       0           1.3
 48       0           1.4
 49       0           1.5
 50       0           1.6
 51       0           1.7
 52       0           1.8 
package com.ashok.jvm.test;

import java.io.*;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ClassVersionChecker {
    private static void checkClassVersion() throws Exception {
        DataInputStream in = new DataInputStream(new FileInputStream("D://Ashok /Test.class"));
        int magic = in.readInt();
        if (magic != 0xcafebabe) {
              System.out.println(" It is not a valid class!");
        }
        int minor = in.readUnsignedShort();
        int major = in.readUnsignedShort();
        System.out.println( major + " . " + minor);
        in.close();
   }
   public static void main(String[] args) throws Exception {
        checkClassVersion();
   }
}

Note

Higher version JVM can always run class files generated by lower version compiler but lower version JVM can’t run class files generated by higher version compiler. If we are trying to run then we will get run time exception saying UnsupportedClassVersionError:Test : unsupported major.minor version.

constant_pool_count

The value of the constant_pool_count item is equal to the number of entries in the constant_pool table plus one.The constant pool table is where most of the literal constant values are stored. This includes values such as numbers of all sorts, strings, identifier names, references to classes and methods, and type descriptors.

constant_pool[]

It represents information about constants present in the constant table. The constant_pool is a table of structures representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool table entry is indicated by its first “tag” byte.

CONSTANT_Class

Tag : 7    

Description : The name of a class

CONSTANT_Fieldref

Tag : 9   

Description : The name and type of a Field, and the class of which it is a member.  

CONSTANT_Methodref                

Tag : 10   

Description : The name and type of a Method, and the class of which it is a member.

CONSTANT_InterfaceMethodref 

Tag : 11  

Description : The name and type of a Interface Method, and the Interface of which it is a member.

CONSTANT_String

Tag : 8    

Description : The index of a CONSTANT_Utf8 entry.

CONSTANT_Integer 

Tag : 3    

Description : 4 bytes representing a Java integer.

CONSTANT_Float  

Tag : 4    

Description : 4 bytes representing a Java float.

CONSTANT_Long

Tag : 5    

Description : 8 bytes representing a Java long.

CONSTANT_Double

Tag : 6  

Description : 8 bytes representing a Java double.

CONSTANT_NameAndType        

Tag : 12  

Description : The Name and Type entry for a field, method, or interface.

CONSTANT_Utf8 

Tag : 1  

Description : 2   bytes for the length, then a string in Utf8 (Unicode) format.

access_flags

Access flags follows the Constant Pool. It is a 2 byte entry that indicates whether the file defines a class or an interface, whether it is public or abstract or final in case it is a class. Below is a list of some of the access flags and their interpretation.

Note

A class may be marked with the ACC_SYNTHETIC flag to indicate that it was generated by a compiler and does not appear in source code. 

this_class

Next 2 bytes after access_flags is this_class. It represents the fully qualified name of the current class.

super_class

Next 2 bytes after this_class is super_class. It represents the fully qualified name of the super class.

interfaces_count

Next 2 bytes after super_class is interfaces_count. It represents the number of interfaces implemented by the current class.

interfaces[]

It represents the names of interfaces implemented by the current class.

fields_count

It represents the number of fields present in the current class.

fields[]

It represents field information present in the current class.

methods_count

It represents the number of methods present in the current class.

methods[]

It represents method information present in the current class.

attributes_count

It represents the number of attributes present in the current class.

attributes[]

It represents attribute information present in the current class.

That’s it guys. This is all about JVM Tutorial. Let me know your comments and suggestions about this tutorial. Thank you.

Class File Structure


Scroll to top