Java Reference
In-Depth Information
for ( Class <?> k : ancestors ) {
if ( c . equals ( k )) return c ;
}
c = c . getSuperclass ();
}
return Object . class ;
}
Class files have a very specific layout that they must conform to if they are to be
legal and loadable by the JVM. The sections of the class file are (in order):
g
R e l e c t i o n
d
• Magic number (all class files start with the four bytes CA FE BA BE in hexadeci‐
mal)
• Version of class file standard in use
• Constant pool for this class
• Access flags ( abstract , public , etc.)
• Name of this class
• Inheritance info (e.g., name of superclass)
• Implemented Interfaces
• Fields
• Methods
• Attributes
The class file is a simple binary format, but it is not human readable. Instead, tools
like javap (see Chapter 13 ) should be used to comprehend the contents.
One of the most often used sections in the classfile is the Constant Pool—this con‐
tains representations of all the methods, classes, fields and constants that the class
needs to refer to (whether they are in this class, or another). It is designed so that
bytecodes can simply refer to a constant pool entry by its index number—which
saves space in the bytecode representation.
There are a number of different class file versions created by various Java versions.
However, one of Java's backward compatibility rules is that JVMs (and tools) from
newer versions can always use older class files.
Let's look at how the classloading process takes a collection of bytes on disk and
turns it into a new class object.
Phases of Classloading
Classloading is the process by which a new type is added to a running JVM process.
This is the only way that new code can enter the system, and the only way to turn
data into code in the Java platform. There are several phases to the process of class‐
loading, so let's examine them in turn.
Search WWH ::




Custom Search