Static control flow

Static control flow from parent to child:


A set of specific rules to read the program from top to bottom.

In case of static control flow, the following operations are performed
  • Identification of all static members within a program from top to bottom, from parent to child
  • Execution of static variable assignment and static block from parent to child
  • Execution of main method


Example 1;
class Test //
{
static int i=10; //1,2 as i=0
static{//2,3
m1();//4
System.out.println(“First static block”);                      //7
}
public static void main(String args[]) //11
{
m1();//12
System.out.println(“Main method”);//15
}
public static void m1()//5,13
{
System.out.println(j);//6 (as 0 (Initialization)), 14
}
static//3,8
{
System.out.println(“Second static block”);//9
}
static int j=20;//4,10 (now j is 20),
}
Output:
0
First static block
Second static block
20
Main method

The first step is, it will initialize the static members,
Then it will execute the static variables and static blocks
Then executes main method

Read Indirectly write only state (RIWO)
  • Whenever JVM identifies a variable, immediately it will assign a value to the variable and that variable state is called RIWO.
  • Now there are two types of RIWO’s
  • Direct read operation (DRO) and indirect read operation (IRO)
  • Within a static block, if we are reading a variable directly, then its DRO
  • A static block referring to a method, by calling, then it’s
  • With RIWO, if we are doing a DRO, then it’s a compilation error

Example for DRO:
class Test
{
static
{
System.out.println(i);
}
public static void main(String… args)
{
}
static int i=0;
}
Here we are asking for i value directly from static block, which is not the way.
Output: Compilation error

Example for IRO:
class Test
{
static
{
m1();
}
public static void main(String… args)
{
}
public static void m1()
{
System.out.println(i);
}
static int i=0;
}
Output: 0

This process of execution (Static control flow, I mean) is same for any program, with parent classes included too.
Let’s look at an example:
class Test
{
static
{
m1();
System.out.println(“Parent static block”);
}
public static void main(String… args)
{
}
public static void m1()
{
System.out.println(i);
}
static int i=0;
}
public class child extends Test
{
static int x=30;
static
{
m1();
System.out.println(“Child static block”);
}
public static void main(String… args)
{
m1();
System.out.println(“Main Child”);
}
public static void m1()
{
System.out.println(y);
}
static int y=40;
}
Output:
0
Parent static block
0    
Child static block
40
Main Child


General info for Static blocks
  • They will be executed at the time of class loading
  • To perform any operation at the time of class loading, we need to write static blocks.
  • In general, at class loading, native libraries of a program will be initialized.

Example:
class Test
{
static
{
System.out.println(“Native Libaray path”);
}
}

A small point related to JDBC, a driver class will be registered with driver manager, where a the logic will be written in static block.
class Driver
{
static
{
//Registerd driver class with Driver Manager
}
}
If you don’t understand this, just ignore, Tricky will explain later.
Note: From 1.7 version, main method is mandated, so following programs are valid in 1.6, but not in 1.7
Now an important question.
Can we print a statement in console, without writing main method?
Yes, Its possible, by using static blocks
class Test
{
static
{
System.out.println(“Hello”);
System.exit(0);
}
}
Output: Hello
Now, without using main method and static block, can we print anything in console?
Yes, ta da!!!!!!!!
With static variables, DUH!!!!!

class Test
{
static int i=m1();
static int m1()
{
System.out.println(“Hello”);
return 0;
}
}
Output: Hello
class Test
{
static Test t=new Test();
Test()
{
System.out.println(“Hello”);
}
}
Output: you guess!

Post a Comment

Thank You

Previous Post Next Post