Showing posts with label static methods. Show all posts
Showing posts with label static methods. Show all posts

Wednesday, December 12, 2018

Java OOPS: Static Control Flow


Hi ,

This post is related to the static control flow. Before starting we should have the clear understanding of what is static block, how it is written in our java code. So for that please check this wonderful tutorial : static keyword in java

Now the point is what is static control flow. When everything is static in our class from top to bottom , what will be the execution flow , of that class. This will be known as Static Control Flow.

First I will show one example of this type scenario , then I will explain about the rules , which we have to follow to execute and then will implement those rules in my example to get the final output.

Example:

public class StaticControlFlowDemo {

static int i = 10;

static{
     m1();
     System.out.println("First static block!");
}

public static void main(String[] args){
     m1();
     System.out.println("Main Method");
}

public static void m1(){
     System.out.println(j);
}

static{
     System.out.println("Second static block!");
}

static int j =20;

}

The above is a simple java class, where all the members of the class are static. Now in this class , we will check what will be output and why? Which stepsl should be followed behind that output?

Whenever we are executing a java class , the following sequence of steps/rules  will executed , as the part of Static Control Flow: 


  • Rule 1 : Identification of static members from Top to Bottom  
  • Rule 2 : Execution of static variable assignment and static blocks from top to bottom 
  • Rule 3: Execution of main method 


=> Firstly we need to identify the static members from top to bottom 

=>In Second step we have to execute all the static variables and static blocks /members from top to bottom

=>In Third step we will execute the main method.

In this way we will execute above java class. I would suggest to write this code in a hard paper and apply the steps one by one , for better clarity. I have tried the same rules, please take a look on below image :



I have mentioned all the steps and with different colors : 

RULE 1 

RULE 2

RULE 3 
Here I have used two states for variables [RIWO] and [R&W] states, full form of these states are :

RIWO -> Read Indirectly & Write Only  (Initial State)
R&W -> Read and Write  

These state I will explain later.

With above formatting I have added the steps in above image. Please take a look . After applying the steps  we will get the following output:

Output :


0
First static block!
Second static block!
20
Main Method

And if we compile the code , it will show same output which I have mentioned.


Inside a static block if we are trying to read a variable that read operation is called Direct Read.
If we are calling a method , and within that method if are trying to read a variable , that read operation is called InDirect Read.

public class Test {
static int i = 10;
static{
m1();
System.out.println(i);     //Direct Read
}
public static void m1(){
System.out.println(i);     //Indirect Read
}
}



If a variable is just identified by the JVM , and the original value not yet assigned . Then the variable is said to be in READ INDIRECTLY & WRITE ONLY STATE . [RIWO]

If a variable is in RIWO state , then we can't perform Direct Read, but we can perform Indirect Read. 

Example 1:


public class StaticControlFlowDemo {
static int i = 10;
static{
System.out.println(i);  
//Direct Read
}
public static void main(String[] args){
System.out.println("Main Method");
}
}
Output :
10
Main Method


Example 2:

public class StaticControlFlowDemo {
static{
System.out.println(i);
}
public static void main(String[] args){
System.out.println("Main Method");
}
static int i = 10;
}

Output :

error: illegal forward reference




Example 3:

public class StaticControlFlowDemo {
static {
m1();     
//Indirect Read
}
public static void m1(){
System.out.println(i);
}
public static void main(String[] args){
System.out.println("Main Method");
}
static int i = 10;
}
Output :

0
Main Method

So in this way , static control flow works in java. I would suggest , to compile this code by yourself and apply the rules which is explained and check the output for better understanding. 

Java OOPS: Inheritance (Overriding) Part 6 With Static Methods

Hi All ,

In previous post we checked how various methods works in overriding. Now in this post we will take a look how static method works in overriding concept.

Case I :  

Let's take a look:

public class InheritanceDemo1 {
public static void main(String [] args){
Child child = new Child();
child.m1();
}
}
class Parent {
public static void m1(){
System.out.println("This is parent class m1 method");
}
}
class Child extends Parent {
public void m1(){
System.out.println("This is child class m1 method");
}
}

Output :

error: m1() in Child cannot override m1() in Parent
overridden method is static


Explanation:  

In this example we can see that we can't override a static method as non-static , otherwise we will get compile time error. 



Case II :  


Same  previous case , we are going to check , with overriding the non-static method from Parent to static Child class method .Let's take a look:

public class InheritanceDemo1 {
public static void main(String [] args){
Child child = new Child();
child.m1();
}
}
class Parent {
public void m1(){
System.out.println("This is parent class m1 method");
}
}
class Child extends Parent {
public static void m1(){
System.out.println("This is child class m1 method");
}
}


Output :



error: m1() in Child cannot override m1() in Parent

overriding method is static

Explanation:  

In this example we can see that we can't override a non-static method as static , otherwise we will get compile time error. 




Case III :  


Now in this case we will check if both parent and child class methods are static then , we won't get any compile time error . It seems overriding concept applicable for static methods ,but it is not overriding and it is Method Hiding .Let's take a look:

public class InheritanceDemo1 {
public static void main(String [] args){

Parent parent1 = new Child();
parent1.m1();

Parent parent2 = new SubChild();
parent2.m1();
}
}
class Parent {
public static void m1(){
System.out.println("This is parent class m1 method");
}
}
class Child extends Parent {
public static void m1(){
System.out.println("This is child class m1 method");
}
}
class SubChild extends Parent {
public static void m1(){
System.out.println("This is sub child class m1 method");
}
}

Output :

This is parent class m1 method
This is parent class m1 method

Explanation:  

In this example we can see that it is method hiding , method resolution is always takes care by compiler based on reference type. 

Hidden methods are in Static context, I believe. Static methods are not overridden, because the resolution of method calls done by the compiler at the compile time itself. So, if you define a static method in the base class with the same signature as that one present in the parent class, then the method in the subclass hides the method inherited from super class.

If both Parent and Child class methods are non-static , then it will become overriding .In that case output will be :

Output :

This is child class m1 method
This is sub child class m1 method