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 – 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].

Regular Expression – Part 2

Regular Expression

Video:

Character Classes
[abc] – either a , b or c
[^abc] – except a, b, c
[a-z] – any lower case alphabet symbol from a to z
[A-Z] – any lower case alphabet symbol from A to Z
[0-9] – Any digit from 0 to 9
[a-zA-Z] = Any alphabet
[0-9] – Any digit from 0 to 9
[0-9a-zA-Z] = Any alphanumeric symbols
[^0-9a-zA-Z] = Special symbol except alphanumeric symbols

Pre-defined Character Classes
\s – Space character
\S – Expect Space character
\d = Any digit from 0 to 9 [0-9]
\D = Except digit, any character
\w = Any word character [0-9a-zA-Z]
\W = Except word character [special character]
. = Any character

Quantifiers – We can use quantifiers to specify the number of occurrences to match
a = Exactly one ‘a’
a+ = atleast one ‘a’
a* = Any number a’s including zero number
a? = Almost one ‘a’

Java Example –