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.

Slides:



Advertisements
Similar presentations
8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
CS884 (Prasad)java7ExprSt1 Statements and Expressions Execution.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
Outline Java program structure Basic program elements
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
Initialization George Blank Subba Reddy Daka. Importance of Initialization Java classes are initialized and have predictable default values. These values.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Memory Model of A Program, Methods Overview l Memory storage areas for an executing program l Introduction to methods and methods definitions l General.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming Elhanan Borenstein Lecture #4.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture 10 : Introduction to Java Virtual Machine
The Java Programming Language
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Utilities (Part 2) Implementing static features 1.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Programming in Java CSCI-2220 Object Oriented Programming.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Programming Languages and Paradigms Activation Records in Java.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Methods.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
RealTimeSystems Lab Jong-Koo, Lim
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Design issues for Object-Oriented Languages
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Chapter 3: Using Methods, Classes, and Objects
Topic: JAVA .Class File Structure
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Object Oriented Programming in java
Classes and Objects Object Creation
Corresponds with Chapter 5
CMSC 202 Constructors Version 9/10.
Presentation transcript:

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 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 Java Program at Compile Time and Runtime

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