Professional Documents
Culture Documents
Oop - Java - Pg-Desd - Session 3
Oop - Java - Pg-Desd - Session 3
Session 3:
Learning Objectives
Explain this facility Describe static member, method and block
this keyword
It is a reference variable that refers to the current object Call to this() must be always first
Usage:
1. Used to refer current class instance variable 2. this() is used to invoke current class constructor 3. Used to invoke current class method 4. Can be passed as an argument to the method 5. Used as argument in constructor call 6. Used to return current class instance
Advantage of this()
Proving this
Static means global--all objects refer to the same storage. applies to variables or methods usage:
with an instance variable of a class with a method of a class
Static Block
javap - to print the Java bytecodes javadoc - documentation generator javah - creates C header files
The following figure shows a block diagram of the JVM that includes its major subsystems and memory areas.
As the program runs, all objects instantiated are stored in the heap
The stack area is used to store activation records as a program runs
The linking process consists of three sub-tasks, namely, verification, preparation, and resolution
Note that for any loaded type T, only one instance of java.lang.Class is created even if T is used several times in an application. To use the above methods, we need to first call the getClass() method on any instance of T to get the reference to the Class instance for T.
Preparation
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(ABC); 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); ... }
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; ... }
Garbage Collection
Agenda:
What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy
What is Garbage?
Garbage: unreferenced objects
Ram Object
ram
shyam
Shyam Object
Example
Object p= new Integer(57); Object q = p;
57 refCount = 0
q 99 refCount = 2
head
ListElements next
refCount = 1
ListElements next
refCount = 1
ListElements next
refCount = 1
Disadvantages
Reference counting does not detect garbage with cyclic references. The overhead of incrementing and decrementing the reference count each time. Extra space: A count field is needed in each object. It may increase heap fragmentation.