final keyword

Video tutorial:

Final Instance Variables:
1. If the value of a variable is varied from object to object such type of variable is called instance variables.
2. For every object, a separate copy of instance variable will be created.
3. For instance variable we are not required to perform initialization explicitly, JVM will always provide default values.
4. If instance variable declared as final then compulsory we have to perform initialization compulsory whether we are using or not and JVM won’t provide default values.

Rule: For final instance variable compulsory we should perform initialization before construction completion.
The following are various places for initialization
a. At the time of declaration
b. Inside instance block
c. Inside constructor
If we are trying to perform initialization anywhere else then we will get compile time error.

Final Static Variables:
If the value of the variable is not varied from object to object such type of variable is not recommended to declare as instance variables, we have to declare those variables at class level by using a static modifier.

In the case of instance variables for every object 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 that class.

For static variable it is not required to perform initialization explicitly, JVM will also provide default values.

If the static variable declares as final compulsory we should perform initialization explicitly otherwise we will get compile time error and JVM won’t provide any default values.

Rule: For final static variable initialization compulsory Before Class loading completion
1. At the time of declaration
2. Inside Static Block

These are the only possible for performing initialization for a final static variable, if we are trying to perform initialization anywhere else then we will get compile time error.

Final Local Variables:
Sometime to meet the temporary requirement of the programmer we have to declare variables inside a method or block or constructor such type of variables is called Local Variables or Temporary Variables or Stack Variables or Automatic variables.

* For local variable JVM won’t provide any default values compulsory we should perform initialization explicitly.

* Even though the local variable is final before using only we have to perform initialization, if we are not using then it’s not required to perform initialization even though it is final.

* The only applicable modifier for a local variable is final by mistake if we are trying to apply any other modifier then we will get compile time error.

* If we are declaring any modifier then by default is it default but this is the rule is applicable only instance and static variables but not for local variables.

* Formal parameter of a method simply acts as a local variable of that method hence formal method can be declared as final. If formal parameter declared as final then with-in a method we can’t perform reassignment.

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.

Threads – Thread Group

Thread Group is used to set the capability to pool of thread which is associated with the group in one go. Here are the rules

* If thread group priority is less than the priority of the threads then threads priority will reset. e.g. If thread group priority is 8 and thread to 9. Then thread priority will rest to 8.
* If thread priority set before setting thread group priority then it will remain same.
* If thread priority is less than the thread group priority then it will remain same.

Please share your experience at online.nimit@gmail.com