Friday, February 20, 2009

Java execution flow

Before knowing about complete java execution flow we have to know about the
static control flow (static variables,blocks) and instance control flow (instance variables ,blocks,constructors) flow.
STATIC CONTROL FLOW:

1. Identification of static members with default value assignment.i=0,J=0.
2. Execution of static variable assignments and static block. i=10, j=20.
3. Execution of main method.
Example :
class Base
{
1--> static int i=10; -->7
2--> static
{
m1();-->8
// System.out.println(j); //CTE: Illegal Forward Exception,
// System.out.println(i); //10
System.out.println(“First Static Block”); --> 10
}
3 --> public static void main(String a[])
{
m1(); --->13
// System.out.println(j); //20
System.out.println(“main method”); --->15
}
4 --> publis static void m1()
{
System.out.println(j); ---> 9,14
}
5 --> static
{
System.out.println(“second static block”); -->11
}
6 --> static int j=20; ---> 12
}
(In The above program 1,2,3...... numbers 1 for jvm exicution start there and so on)

Output :
0
FSB
SSB
20
Main method

Note: - If the static of any variable is Read indirectly write only then we can’t perform read operation directly violation leads to compile time error saying “Illegal Forward Reference”.
In jdbc Load the driver, Create connection, create stmt, Result set….we will get these type of error.
Static blocks:
• While the class loaded into the memory if you want to perform some mandatory actions, then we can keep those statements into static blocks.
Take for example
1. Loading native libraries,
2. Registering DBdiver with the DriverManager.

Static Control Flow in the Parent and Child class Relationship(inheritance hierarchy):
1. Identification of static members from parent to child with default values i=0 ,j=0,x=0.
2. Execution of static variable assignments and static blocks from
parent to child.i=10, j=20, x=100, y=200.
3. Execution of main() of derived class.

Example:
class Basic
{
1 --> static int i=10; -->12
2 --> static
{
m1(); --->13
S.o.p(“Base static block”); --->15
}
3 --> public static void main(String a[])
{
S.o.p(“Base main method”);
}
4 --> public static void m1()
{
S.o.p(j); -->14
}
5 --> static int j=20; -->16
}
class Derived extends Base
{
6 --> static int x=100; --->17
7 --> static
{
m2(); --->18
S.o.p(“Derived First static block”); --->20
}
8 --> public static void main(String a[])
{
m2(); -->23
S.o.p(“Derived main”); --->25
}
9 --> public static void m2()
{
S.o.p(y); -->19,24
}
10--> static
{
S.o.p(“Derived second static block”); -->21
}
11 --> static int y=200; -->22
}

Save: Derived.java
Compile: javac Derived.java
Run: java Derived

output :
0
BSB
Base main
• If there is no main() in derived / child class, ouput:
0
BSB
0
DFSB
DSSB
Base main method

• Because parent class main() will execute. So, static methods can be inherited(including main()) but can’t be overridden.

Instance Control Flow:
Instance Control Flow In Single Class:
1. Identification of instance members. i=0, j=0
2. Execution of instance variable assignments and instance blocks. I=0 j=20
3. Execution of Construction
Example:
class Parent
{
1--> int i=10; <--7
{
2--> m1(); <--8
System.out.println(“First instance initialization block”); <--10
}
3--> Parent()
{
System.out.println(“constructor”); <--12
}
public static void main(String a[])
{
System.out.println(“main method”); <-- 13
Parent p=new Parent();
Parent p1=new Parent();
}
4--> public static void m1()
{
System.out.println(j); <-- 9
}
5--> {
System.out.println(“second instance initialization block”); <-- 11
}
6--> int j=20;
}
Instance Control Flow in the Inheritance hierarchy:
1. Identification of instance members from parent to child.
2. Execution of instance variable assignments and instance blocks only in the parent class
3. Execution of parent class Constructor.
4. Execution of instance var assignments and instance blocks in the child class.
5. Execution of child class Constructor.

Example:
class Sample
{
int i= 0;
public static void main(s[] a)
{
System.out.println(i);
}
}
output:compile time error: instance variable can’t be referred from static content.
• Instance var can’t be refered from static context directly, because they willn’t be identified by the JVM.

Java execution flow:
Know we will easy to identify the flow of the java program see the below examples and outputs.

What will be result of attempting compile and run the fallowing code?
public class Myclass
{
public static void main(s[] a)
{
Myclass obj= new Myclass(l);
}
static int i=5;
static int l; int j=7; int k;
public Myclass(int m)
{
System.out.println(i+”,”+j+”,”+k+”,”+l+”,”+m);
}
{
J=70; l=20; //Instance initialize block
}
static
{
i=50;
} //static Initialize block
}
Output: 50,70,0,20,0

Q). Find the o/p of the fallowing program.
public class Initialization
{
private static String msg(String msg)
{
System.out.println(msg); return msg;
}
public Initialization()
{
m= msg(“1”);
}
{
m=msg(“2”);
}
String m=msg(“3”);
public static void m(s[] a)
{
Object o=new Initialization();
}
}
Output:- 2
3
1

Example:
public class Initialization
{
Private static String msg(String msg)
{
System.out.println(msg); return msg;
}
static String m=msg(“1”);
{
m=msg(“2”);
}
static {
m=msg(“3”);
}
public static void main(String a[])
{
Object o=new Object();
}
}
Output: 1
3
2

1 comments:

Unknown said...

Nicely described

Post a Comment