Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.

Similar presentations


Presentation on theme: "1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects."— Presentation transcript:

1 1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects l Example of Object Creation

2 2 Steps in Executing a Java Program l 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) l In this lecture, we explore some of the activities carried out by the JVM. l These includes Loading, Linking, Initialization and Creation of class instances:

3 3 Java Program at Compile Time and Runtime

4 4 Loading l Loading refers to the process of finding the binary form of a class with a particular name and loading it into memory l This process is implemented by a module of the JVM called Class Loader. l 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. l Because the loading process involves memory allocation, it could also result in OutOfMemoryError.

5 5 Linking l 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. l Linking involves three different activities, namely: Verification, Preparation and Resolution of Symbolic references. Verification: l This is the process of ensuring that binary representation of a class is structurally correct. l 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. l If an error occurs, an instance of VerifyError is thrown.

6 6 Initialization l Initialization of a class consists of executing its static initializers and initialization of its static fields. l Before a class is initialized, its super class must be initialized if it has not been previously initialized. l 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. l There is also the possibility that initialization of a class may be requested recursively as part of initialization of the same class. l JVM is responsible for taking care of synchronization and recursive initialization.

7 7 Creation of Class Instances l When a new class instance is created, memory is allocated for all its instance variables. l Memory is also allocated recursively for all the instance variables declared in its super class. l All instance variables in the new object including those of the super class are then initialized to their default values. l The indicated constructor is then processed according to the rules shown on the next slide. l Finally, the reference to the newly created object is returned as the result.

8 8 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.

9 9 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.

10 10 Rules for processing a Constructor 1. Assign the arguments for the constructor to the newly created parameter variables for this constructor invocation. 2. 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. 3. 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. 4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables. 5. Execute the rest of the body of this constructor.

11 11 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) { // (3) System.out.println("Non-default constructor in SubclassB"); value = i; } { // (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) }

12 12 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.

13 13 Object Creation Example l 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); }

14 14 Object Creation Example (Cont’d) l First, space is allocated for the new ColoredPoint object, to hold the fields x, y, and color. l All these fields are then initialized to their default values (in this case, 0 for each) l 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. l 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; } l Therefore, the constructor for Object which takes no arguments is invoked. The class Object has no superclass, so the recursion terminates here.

15 15 Object Creation Example (Cont’d) l Next instance variable initializers of Object are invoked. l 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() { } l This constructor executes without effect and returns. l 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. l Then the body of the Point constructor is executed, setting both x and y to 1. l Next, the initializers for the instance variables of class ColoredPoint are executed. This step assigns the value 0xFF00FF to color. l 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


Download ppt "1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects."

Similar presentations


Ads by Google