Variables in Java
- Variables in Java are used to store data.
- Value of variable may be change during program execution.
- But name of variable remain the same.
- Data stored in variables are temporary because variables created in RAM.
- When Program ends, data in the variable automatically removed.
- There are three types of variables in Java:
1- Local Variable
2- Member Variable
3- Static Variable
Local Variables in Java
Local variables defined only in the local area.
valid only in the area belongs within functions, statements within the like.
Local variables on the stack memory, at the end of the range effect, variable space will be automatically released.
Local variables in Java have no default initialization value.
Member Variables in Java
Member variables are also known as instance variables.
Member variables in Java are defined in the class so the whole class can be accessed.
Member variables allocated on the heap.
Member variable has a default initialization values.
Static Variables in Java
Static variables also called class variables.
Static variables can be accessed through the class name.
There is no need to create a class object to access static members of the class.
Static variable in the class is loaded when it will be initialized. That is, as long as the class is loaded, regardless of whether you use the static variable, it will be initialized.
Static variables in Java are initialize with word "static".
Example of variables in Java
int a = 10; `//This is Local variable, its scope is within this function
int b = 25; //This is Member variable
static int c = 50; //This is Static variable
Major Difference between normal data member and static data member
Each object has its own variable of normal data member. But static data member is shared among all the objects of the class. For static data member, only a single memory location is created which is shared among all the objects.