Welcome to all
access modifier data type variable name = value;
There are three types of variable in java
Variables
The variable is the name of the memory location which may contain the value.
variable have specific data type in java so based on that memory is allocated to the variable.
access modifier data type variable name = value;
There are three types of variable in java
- local variable
- instance variable
- static variable
local variable
- local variables are those variables which are declared inside the method, loop, constructor, and blocks.
- local variable's scope or life is for that loop, block or method only in which they have declared.
- There is no default value for a local variable so we have to initialize it with any value before using it.
- All local variables are stored inside the stack memory.
- local variable destroys when loop or block gets over.
stack |
instance variable
- It is declared inside the class but outside the method or loop and constructor.
Inbuilt Structure - When the object is created instance variable also created itself.
- When the object is destroyed instance variable also destroyed itself.
- instance variables are stored in heap memory.
- They have default value according to the data type of variables.
- We can use instance variable directly inside the class but outside the class we need to create an object of the class if the variable is public otherwise we can access it using getter method of that variable.
example
output |
static/class variable
- Those variables are declared with static keyword are called static variables which are outside the block or loop and inside the class means instance variable.
- Only one copy per class of variable.
- Stored in static memory.
- variable has a default value.
- We can access it by calling with the classname.
- When declaring the variable as public static final then variable names are all in UPPERCASE.
Diagram of static variable Example output
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments
Please comment here...