Java Variables
-----------------------
- Variable is a name of memory location.
- A variable is the name of a reserved area allocated in memory. In other words,
it is a name of
the memory location.
- It is a combination of "vary + able" which means its value can be changed.
- A variable is a container which holds the value while the Java program is
executed. A variable
is assigned with a data type.
- based on position of declaration and behaviour all variables divided into 3
types
1) Local Variable
2) Instance Variable
3) Static variable
1) Local Variable:
-----------------------
- Some times to meet temporary requirements of the programmer we can declare
variables inside a method
or block or constructors such type of variables are called local variables or
temporary variables
or stack variables.
- Local variables will be stored inside stack.
- The local variables will be created as part of the block execution in which it
is declared and destroyed
once that block execution completes. Hence the scope of the local variables is
exactly same as scope
of the block in which we declared.
i) Declaration : inside a method or block or constructors
ii) Scope(access) : same as method or block or constructors
iii) Stored Memory : stack memory
iv) Default value : Doesn't have default value
v) when memory allocate: when method or block or constructor call
vi) How to access : using variable name or directly
2) Instance Variable:
----------------------------
- If the value of a variable is varied from object to object such type of
variables called Instance
variable
- For every object seprate copy of instance variable created.
- A variable declared inside the class but outside the body of the method or block
or constructor is called an instance variable.
It is not declared as static.
- Instance Variable should be declare within class directly but outside of any
constructor, block ,method.
- The memory allocation will be at time of object creation and distroy at the time
object distruction.
- the scope of Instance Variable is same as object.
i) Declaration : inside the class but outside a method or block or
constructors
ii) Scope : inside all method or block or constructors and within class.
iii) Stored Memory : Heap Memory
iv) Default value : instance variable have default value (depend on data type of
variable)
v) when memory allocate: when object is created and when object deleted varible
also destroyed
vi) How to access : using object reference
3) Static variable:
-----------------------
- If the value of a variable is not varied from object to object such type of
variables is not
recommended to declare as instance variables. We have to declare such type of
variables at class
level by using static modifier.
- In the case of instance variables for every object a separate copy will be
created but in the case of
static variables for entire class only one copy will be created and shared by
every object of that class.
- Static variables will be crated at the time of class loading and destroyed at
the time of class unloading
hence the scope of the static variable is exactly same as the scope of
the .class file.
- Static variables will be stored in method area. Static variables should be
declared within the class
directly but outside of any method or block or constructor
- Static variables can be accessed from both instance and static areas directly.
- We can access static variables either by class name or by object reference but
usage of class name
is [Link] within the same class it is not required to use class name we
can access directly
i) Declaration : inside the class with static keyword but outside a method or
block or constructors
ii) Scope : inside all method or block or constructors and within class.
iii) Stored Memory : Non-Heap, static memory
iv) Default value : static variable have default value
v) when memory allocate: when we run programme and .class file will loaded then
static varibales will be
allocated.
when .class file gets unload static variable get destroyed
vi) How to access : directly, directly with class name, by using object reference