Static Variable

Video:

Static variables

* If the value of a variable is not varied from object to object then it is not recommended to declare a variable as an instance variable. We have to declare such type of a variable at class level by using a static modifier.

* In the case instance variables for every object as a separate copy will be created but in the case of static variables a single copy will be created at the class level and shared by every object of the class.

* Static variable should be declared within the class directly but outside of any method, block or constructor.

* Static variable will be created at the time of class loading and destroyed (at the time of class unloading), hence the scope of the static variable is exactly same as the scope of the .class file. Internal steps for the load to unload a class
a. Start JVM
b. Create and start main Thread.
c. Locate .class file
d. Load .class (static variable creation)
e. Execute main() method
f. Unload .class (static variable termination)
g. Terminate main Thread h. Shutdown JVM

* Static variable will be stored in method area.

* We can access static variables either by object reference or by className but recommend to use className.

* Within the same class is not required to use class name and we can access directly.

* We can access the static variable directly from both instance and static areas.

* For static variable JVM will provide default values and we are not required to perform initialization explicitly.

* Static variable also known as class level variable or fields.

Static Control Flow – 2

Video LInk :

Static Block
1. Static Blocks will be executed at the time of Class Loading hence at the time class loading if we want to perform any activity we have to define that inside a static block.
2. At the time of Java class loading the corresponding native library should be loaded hence we have to define this activity inside a static block.
3. After Loading every database driver class we have to register driver class with driver manager but inside database driver class there is a static block to perform this activity and we are not responsible to register explicitly
4. Within a class, we can declare any number of a static block but all this static block executed top to bottom.

*There are multiple ways to print statement without static block and the main method.
*From 1.7 version onwards main method is mandatory to start a programme execution. Hence, *from 1.7 versions onwards without writing the main method it is impossible to print some statement to the console.

Static Control Flow in Parent to Child relationship

Whenever we are executing child class the following sequence of event will be executed automatically as the static control flow
1. Identification of static member from parent to child.
2. Execution of static variable assignment and static blocks from parent to child.
3. Execution of only child class main method.

Whenever we are loading child automatically parent class will be loaded but whenever we are parent class child class won’t be loaded [because parent class members by default available to the child class whereas child class members by default won’t available to the parent].

Static Control Flow – Part 1

Video Link:

Static Control Flow
1. Identification of Static members from top to bottom
I=0 [RIWO] Read Indirectly Write only state
j=0 [RIWO]
2. Execution of Static variable assignment and static blocks from top to bottom
I=10 [R&W] Read and Write
j=20 [R&W]
3. Execution of main methods

Whenever we are executing a Java class the following sequence of steps will be executed as the part of static control flow
1. Identification of Static members from top to bottom [ 1 to 6]
2. Execution of Static variable assignment and static blocks from top to bottom [ 7 to 12]
3. Execution of main methods [13 to 15]

Read Indirectly Write Only
1. Inside a static block we are trying to read a variable that read operation is called Direct Read.
2. If we are calling a method and with-in that method if we are trying to read a variable that read operation is called In-direct read.
3. If a variable is just identified by JVM and original value not yet assigned then the variable is said to be in RIWO Read Indirect Write only state
4. If a variable is in RIWO state then we can’t perform direct Read but we can perform indirect read. If we are trying to read directly we will get compile Time error illegal forward reference.