QA's approach 2 Java - Understanding Static context

            Static Members Flow

Class members

In java application we are defining objects for communication.Every Object is represented as a model(class). Hence, java application is the combination of classes. And every class in java application is allowed to define following members.

1) static members
2) non-static members


Static members flow using JVM architecture :
Java class supports definition of 4 static members.

1) static main method
2) static block
3) static variable
4) static method

Static main method Example

class Demo
{
//empty.....
}

How to Compile & Execute a simple Java program?

1) Save the program in "d-drive" with name Demo.java
2) Open command prompt, navigate to the path where the java program is stored
3) Compilation:
cmd/> javac Demo.java

Demo.class file will be generated.

4) Execution: 
cmd/>java Demo

Error : main method not found...
main method :

1) It is static method, hence no need to call it explicitly.
2) Every java application execution starts from main method.
3) JVM invokes main method implicitly
4) main method is mandatory in Execution logic class, but not mandatory for Business login class. Hence at the Time of compilation, no error message will be received, but when executing, the Error message will be received.


class Demo
{
public static void main(String args[ ])
{
System.out.println("main method...."); //Debugging: statement is used to analyze whether a block or method is executing or not.
}
}

static block:

Block of instructions having no identity.

syntax :

static
{
statement-1;
....
....
statement-n;
}

1. We cannot call static block explicitly(because no identity)
2. JVM invokes the static block implicitly.
3. static block definition is optional.
4. static block executes well before main method irrespective of its definition placed anywhere in the class file, which is shown with an example as below.

static block placed before static main method

class Demo
{
static
{
System.out.println("static block....");
}
public static void main(String args[ ])
{
System.out.println("main method....");
}

}

static block placed before static main method

class Demo
{
public static void main(String args[ ])
{
System.out.println("main method....");
}

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


Ques : Why static block execution is before main method ?
Ans :
1) static block provides basic functionality information of Object
2) main method is used to start communicating with object using block provided information.




Ques : Can we define more than one static block in a single class?
Ans : yes allowed, and all these blocks execute in the defined order. Which can be seen in the example below. First all static blocks gets executed in the order defined in the class file, followed by static main method.


class Demo
{
static
{
System.out.println("static block before main....");
}
public static void main(String args[ ])
{
System.out.println("main method....");
}
static
{
System.out.println("static block after main....");
}
}

Ques : Why definition of n-number of static blocks required in a single class ?
Ans : 
1. Instead of writing the complete page loading info in a single static block, it is better to place in n-number of block.
2. Modularity of programming makes debugging is easy...


static variable:

1. static variable & static block having equal priority, hence these 2 members execute in the defined order.
2. Declaration of variable inside a class and outside to all the methods and blocks. Static variables cannot be defined inside Static blocks as they have equal priority for getting memory. Static variables cannot be defined inside Static methods as they have higher priority for getting memory. Hence static variable is always defined outside blocks & methods but inside the class.
3. static variable gets memory allocation inside the method area.
4. static variable can be accessed using class name.



class Demo

{
static int a ;   
 //static variable & static block have equal priority, hence allocated memory based on the order in which they are placed in class file
static      
//static block is assigned memory after the static variable in here based on the order of placement
{
System.out.println("static block....");
System.out.println(Demo.a);
}
public static void main(String args[ ])
//static method is provided memory post static variable & block
{
System.out.println("main method....");
System.out.println(Demo.a);
}

}




Class Loading & Execution Steps:


1) Method Area provides some space to load the class
2) All the class members will be loaded into method area in the defined order
3) All the static variables get memory allocation inside method area and initializes with default values. JVM allocates default values for all static variables, the default values assigned by default are mentioned as below

Data_type : : Default_value
int  ::  0
float  ::  0.0
char  ::  '\0'
boolean  ::  false

4) JVM set priorities to all the loaded members
5) JVM executes all the members according to priorities.
6) static variables always holds a single value. Shared across class objects. 

Eg: Branch Code of all the customers in a Bank.

static int branch_code = 12345 ;

class Demo

{
public static void main(String args[ ])
{
System.out.println("main method....");
System.out.println(Demo.a);
}
static
{
System.out.println("static block....");
System.out.println(Demo.a);
}
static int a = 100;
}



static user Method :

1. Block of instructions having identity.
2. We must call a method explicitly, using the class name.
3. Every method should have return type(at least void)
4. We can call a static method using its call identity.

syntax :

<static> return_type Identity( )
{
//statements......
}

Example 1: 
class Demo
{

public static void main(String args[ ])
{
System.out.println("main method....");
Demo.fun( );
}
static void fun( )
{
System.out.println("user method....");
}
}

Example 2:
class Demo
{
public static void main(String args[ ])
{
System.out.println("main starts....");
Demo.fun( );
System.out.println("main ends....");
}
static
{
System.out.println("block starts....");
Demo.fun();
System.out.println("block ends....");
}
static void fun( )
{
System.out.println("user method....");
}
}


Different ways to initialize the static variable:

Example 1:

class Demo

{
static int a ;

public static void main(String args[ ])
{
Demo.initialize();
System.out.println(Demo.a);
}

static void initialize( )
{
System.out.println(Demo.a);
Demo.a = 100 ; //initialization.....
}

}


Example 2:

class Demo
{
static int a ;

static
{
System.out.println(Demo.a);
Demo.a = Demo.initialize();
}

public static void main(String args[ ])
{
System.out.println(Demo.a);
}

static int initialize( )
{
return 100 ;
}

}


Example 3

public class Demo
{

static int a = Demo.initialize(); //0

static
{
System.out.println(Demo.a); //100
Demo.a = Demo.a+50 ;
}

public static void main(String args[ ])
{
System.out.println(Demo.a); //150
}
static int initialize( )
{
System.out.println(Demo.a); //0
return 100 ;
}

}

Compilation:



Java Profiling using Javap:


Execution:



Example problems to analyze :


Example 1:

class Pro1

{

static int a = Pro1.fun();

public static void main(String[] args)
{
System.out.println(Pro1.a);
}

static int fun()
{
Pro1.a = 50;
return Pro1.fun1();
}

static int fun1()
{
System.out.println(Pro1.a);
return 100;
}

}


Example 2:

class Pro1
{

static int a = Pro1.fun();
public static void main(String[] args)
{
System.out.println(Pro1.a);
}
static
{
System.out.println(Pro1.a);
Pro1.a = Pro1.a+20 ;
}

static int fun()
{
Pro1.a = 50;
return Pro1.fun1();
}

static int fun1()
{
System.out.println(Pro1.a);
return 100;
}

}


Example 3:

class Pro1
{

static
{
Pro1.a = Pro1.fun();
}

static int a = 70 ;

public static void main(String[] args)
{
System.out.println(Pro1.a);
}

static
{
Pro1.a = Pro1.a+Pro1.fun();
}

static int fun()
{
Pro1.a = 50;
return Pro1.fun1();
}

static int fun1()
{
System.out.println(Pro1.a);
return Pro1.a+30;
}
}






Comments

Popular posts from this blog

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?