Java Variable Declaration & Initialization


Declaring a Variable in Java

☞Variable declaration involves specifying the type of the variable and giving it a name.

Example :

int a;	//variable declaration

Initializing a Variable in Java

☞A variable may be initialized by assigning a specific value to it.

☞Initialization of a variable may take place in two ways:

1.Static Initialization
2.Dynamic Initialization

Static Initialization

☞This process uses direct assignment of a constant to a defined variable (i.e. before its actual use in the programming logic).

☞The variable is initialized at the time of its declaration.

Example :

int a=0;	//variable declaration & static initialization

Dynamic Initialization

☞When a variable get initialized at run time i.e. during the execution of program logic.

☞Under this situation a variable is assigned with the outcome of any arithmetical operation or logical function.

Example :

int a=8,b=5;
int c=a+b;	//variable declaration & dynamic initialization