Transcript Object

Further OO Concepts II – Java Program at run-time

Overview
Steps in Executing a Java Program.

Loading.

Linking.

Initialization.

Creation of Objects.

Example of Object Creation.

Coming Next: Abstract Classes.
Further OO Concepts (Part II)
1
Steps in Executing a Java Program



So far we have learnt that writing java applications involves three
steps:
» Writing the source code
» Compiling the source code to get binary files called bytecode
(.class files)
» Executing the binary files using the Java interpreter called, Java
Virtual Machine (JVM)
In this lecture, we explore some of the activities carried out by the
JVM.
These includes Loading, Linking, Initialization and Creation of class
instances:
Further OO Concepts (Part II)
2
Java Program at Compile Time and Runtime
Further OO Concepts (Part II)
3
Loading




Loading refers to the process of finding the binary form of a class
with a particular name and loading it into memory
This process is implemented by a module of the JVM called Class
Loader.
If the class is found and it is in the correct format, it is loaded,
otherwise the Class Loader throws one of the following errors:
» ClassFormatError: The binary data in the specified class file is
malformed.
» NoClassDefFoundError: The class file does not exists.
Because the loading process involves memory allocation, it could also
result in OutOfMemoryError.
Further OO Concepts (Part II)
4
Linking


Linking is the process of taking a binary form of a class and
combining it into the run-time state of the virtual machine, so that it
can be executed.
Linking involves three different activities, namely: Verification,
Preparation and Resolution of Symbolic references.
Verification:
 This is the process of ensuring that binary representation of a class is
structurally correct.
 For example, it checks that:
» Every instance has a valid instruction code
» Every branch instruction branches to the start (not middle) of another instruction
» Every method has a valid signature, etc.

If an error occurs, an instance of VerifyError is thrown.
Further OO Concepts (Part II)
5
Initialization





Initialization of a class consists of executing its static initializers and
initialization of its static fields.
Before a class is initialized, its super class must be initialized if it has
not been previously initialized.
Because Java is multithreaded (to be discussed later), initialization of
a class requires synchronization, since more than one thread may be
trying to initialize the class at the same time.
There is also the possibility that initialization of a class may be
requested recursively as part of initialization of the same class.
JVM is responsible for taking care of synchronization and recursive
initialization.
Further OO Concepts (Part II)
6
Creation of Class Instances





When a new class instance is created, memory is allocated for all its
instance variables.
Memory is also allocated recursively for all the instance variables
declared in its super class.
All instance variables in the new object including those of the super
class are then initialized to their default values.
The indicated constructor is then processed according to the rules
shown on the next slide.
Finally, the reference to the newly created object is returned as the
result.
Further OO Concepts (Part II)
7
Initialization
If a constant expression is used in the initialization of a member variable then
all its operands must be defined before they can be used itn expression. In the
example below, the initialization at(2) generates a compile time error, because
the operand WIDTH in constant expression has not yet been defined:
Class ConstantInitializerOrder{
private final int LENGTH=10;
//(1)
private double area=LENGTH * WIDTH;//(2)NOT OK,Forward reference
private final int WIDTH=10;
//(3)
If a constant expression is used in the initialization of a member variable, then
all its operands must be defined before they can be used in expression.
The initialization at(2) generates a compile time error, because the operand
WIDTH in the constant expression has not yet been defined.
* A logical error can occur if the order of the initializer expression is not
correct.
Further OO Concepts (Part II)
8
Initialization
Initializers are used in initialization of objects and classes.
Initialization of instance and static variables can be explicitly specified with
their declaration using initializer expressions
class ConstantInitializers{
private int minAge=12;
//(1)
private static double pensionPoints=10.5 //(2)
//.... }
*
Instance variables of an object are initialized with the values of initializer
expressions when the object is created using the new operator
* at(1) will result in the instance variable minAge being initialized to 12 in
every object of the class ConstantInitializers created with new operator. If no
explicit initializer expressions are specified, default values are used to
initialize the in instance variables
* Declaration at (2) will result in the static variable pensionPoints being
initialized to 10.5 when the class is initialized. Again if no explicit initializers
are specified, deault values are used to initialize the static variables.
Further OO Concepts (Part II)
9
Rules for processing a Constructor
1.
2.
3.
4.
5.
Assign the arguments for the constructor to the newly created
parameter variables for this constructor invocation.
If this constructor begins with an explicit constructor invocation of
another constructor in the same class (using this), then evaluate the
arguments and process that constructor invocation recursively.
If this constructor is for a class other than Object, then this
constructor will begin with an explicit or implicit invocation of a
superclass constructor (using super). Evaluate the arguments and
process that superclass constructor invocation recursively.
Execute the instance initializers and instance variable initializers for
this class, assigning the values of instance variable initializers to the
corresponding instance variables.
Execute the rest of the body of this constructor.
Further OO Concepts (Part II)
10
Example
class SuperclassA {
public SuperclassA( ) {
// (1)
System.out.println("Constructor in
SuperclassA");
}
}
class SubclassB extends SuperclassA {
public SubclassB() {
// (2)
this(3);
System.out.println("Default constructor
in SubclassB");
}
public SubclassB(int i) {
System.out.println("Non-default
constructor in SubclassB");
value = i;
}
// (3)
{
// (4)
System.out.println("Instance initializer block in
SubclassB");
// value = 2;
// (5) Not Ok
}
private int value = initializerExpression(); // (6)
private int initializerExpression() {
// (7)
System.out.println("Instance initializer
expression in SubclassB");
return 1;
}
}
public class ObjectConstruction {
public static void main(String args[]) {
new SubclassB();
// (8)
}
}
Further OO Concepts (Part II)
11
Example (Cont’d)
The new operator is used at(8) to create an object of
class SubclassB. The default construcotr Subclassb()
at(2) uses the this() construct to locally chain to the
non-default constructor at(3). It is theis constructor
that leads to an implicit call of the superclass’s
constructor. . This is followed by the execution of the
instance initializerblock at (4) and instance initializer
eexpresion at(6). Then the execution of the body of the
non-default constructor at(3) is resumed. Finally the
default constructor completes its execution.
Further OO Concepts (Part II)
12
Object Creation Example

Consider the following example:
class Point {
int x, y;
Point() { x = 1; y = 1; }
}
class ColoredPoint extends Point {
int color = 0xFF00FF;
}
class Test {
public static void main(String[] args) {
ColoredPoint cp = new ColoredPoint();//default
constructor
System.out.println(cp.color);
}
}
Further OO Concepts (Part II)
13
Object Creation Example (Cont’d)





First, space is allocated for the new ColoredPoint object, to hold the fields x, y, and
color.
All these fields are then initialized to their default values (in this case, 0 for each)
Next, the ColoredPoint constructor with no arguments is first invoked. Since
ColoredPoint declares no constructors, a default constructor of the form:
ColoredPoint() { super(); }
is provided for it automatically by the Java compiler.
This constructor then invokes the Point constructor with no arguments. The Point
constructor does not begin with an invocation of a constructor, so the compiler
provides an implicit invocation of its superclass constructor of no arguments, as
though it had been written: Point() { super(); x = 1; y = 1; }
Therefore, the constructor for Object which takes no arguments is invoked. The
class Object has no superclass, so the recursion terminates here.
Further OO Concepts (Part II)
14
Object Creation Example (Cont’d)







Next instance variable initializers of Object are invoked.
Next, the body of the constructor of Object that takes no arguments is
executed. No such constructor is declared in Object, so the compiler
supplies a default one, which in this special case is: Object() { }
This constructor executes without effect and returns.
Next, all initializers for the instance variables of class Point are
executed. As it happens, the declarations of x and y do not provide any
initialization expressions, so no action is required for this step.
Then the body of the Point constructor is executed, setting both x and
y to 1.
Next, the initializers for the instance variables of class ColoredPoint
are executed. This step assigns the value 0xFF00FF to color.
Finally, the rest of the body of the ColoredPoint constructor is
executed (the part after the invocation of super); there happen to be no
statements in the rest of the body, so no further action is required and
initialization is complete
Further OO Concepts (Part II)
15