There are two types of variable mainly:-
1. Instance Variable or Class Variable
2. Local Variable or method variable
Instance variable are initialized by JVM to their default values if not defined explicitly. Whereas the local variables needs to be defined each time time they are declared. But the local variables can be used to a greater effect by using the concept of dynamic initialization.
Dynamic Initialization can be defined as the dynamic operation that allows variables to be initialized dynamically using any expression valid at the time of the variable declaration.
Above definition implies that if you need a variable to store value of an expression you can use dynamic initialization. In which value of an expression is assigned to a variable.
Dynamic Initialization can be clear by understanding following example:-
class DynamicInit{
public static void main(String a[]){
int a=2;
int b=5;
int c=a*a+b*b;
System. out. println(“value of c is “+c);
}
}
Output :-The output of above program is
value of c is 29
In the above program we have three variables a,b and c. Each has been declared as ‘ int ‘. Variable a and b are declared and provided values on declaration whereas the variable c has been assigned a expression to evaluate and store its value. This is done through dynamic evaluation.
NOTE:-One thing which has to be remembered is that the initialization expression may use any program construct including method calls,instance variables, literals etc.
Topics: Java, Web Development
Submit Your Article





