Lecture 2: Java Virtual Machine (JVM) : Dr. Sapna Malik Asssitant Professor, CSE Deptt.,MSIT
Lecture 2: Java Virtual Machine (JVM) : Dr. Sapna Malik Asssitant Professor, CSE Deptt.,MSIT
Each instance of the JVM has one method area, one heap, and one or
more stacks - one for each thread
When JVM loads a class file, it puts its information in the method area
As the program runs, all objects instantiated are stored in the heap
For each type it loads, the JVM must store the following information in
the method area:
The fully qualified name of the type
The fully qualified name of the type's direct superclass or if the type is an
interface, a list of its direct super interfaces .
Whether the type is a class or an interface
The type's modifiers ( public, abstract, final, etc)
Constant pool for the type: constants and symbolic references.
Field info : name, type and modifiers of variables (not constants)
Method info: name, return type, number & types of parameters, modifiers,
bytecodes, size of stack frame and exception table.
Class Loading Process (Cont’d)
The end of the loading process is the creation of an instance of java.lang.Class
for the loaded type.
The purpose is to give access to some of the information captured in the
method area for the type, to the programmer.
Some of the methods of the class java.lang.Class are:
In this phase, the JVM allocates memory for the class (i.e static)
variables and sets them to default initial values.
Note that class variables are not initialized to their proper initial values
until the initialization phase - no java code is executed until
initialization.
The default values for the various types are shown below:
Resolution
Resolution is the process of replacing symbolic names for types, fields and
methods used by a loaded type with their actual references.
Symbolic references are resolved into a direct references by searching
through the method area to locate the referenced entity.
For the class below, at the loading phase, the class loader would have loaded
the classes: TestClassClass, String, System and Object.
public class TestClassClass{
public static void main(String[] args){
String name = new String(“Ahmed”);
Class nameClassInfo = name.getClass();
System.out.println("Parent is: “ + nameClassInfo.getSuperclass());
}
}
The names of these classes would have been stored in the constant pool for
TestClassClass.
In this phase, the names are replaced with their actual references.
Class Initialization
This is the process of setting class variables to their proper initial values - initial
values desired by the programmer.
class Example1 {
static double rate = 3.5;
static int size = 3*(int)(Math.random()*5);
...
}
Initialization of a class consists of two steps:
Initializing its direct superclass (if any and if not already initialized)
Executing its own initialization statements
The above imply that, the first class that gets initialized is Object.
Note that static final variables are not treated as class variables but as constants
and are assigned their values at compilation.
class Example2 {
static final int angle = 35;
static final int length = angle * 2;
...
}
Class Initialization (Cont’d)
After a class is loaded, linked, and initialized, it is ready for use. Its
static fields and static methods can be used and it can be
instantiated.
When a new class instance is created, memory is allocated for all its
instance variables in the heap.
Memory is also allocated recursively for all the instance variables
declared in its super class and all classes up is inheritance
hierarchy.
All instance variables in the new object and those of its superclasses
are then initialized to their default values.
The constructor invoked in the instantiation is then processed
according to the rules shown on the next page.
Finally, the reference to the newly created object is returned as the
result.
Thanks