Based on the position of declaration & behavior all variables are divided into three parts.
1.instance variable
2.static variable
3.local variable
1.instance variable:-
- If the value of a variable varied from object to object is called Instance variable.
- For every object, a separate copy of instance variable will be created.
- Instance variable should be declared within the class directly, but outside of any method/block/constructor.
- Instance variable will be created at the time of object creation & destroyed at the time of object destruction. So the scope of the instance variable is exactly same as the scope of the object.
- Instance variable will be stored in the HEAP memory as the part of an object.
- We can't access the instance variable directly from STATIC area, but we can access it by using "object reference".
- We can access the instance variable directly from instance area.
example:
For instance variable, JVM will always provide default values.
example:-
2.static variable:-
- If the value of a variable is not varied from object to object then it is not recommended to declare the variable as the instance variable.We have to declare that variable static variable.
- In the case of a static variable, a single copy will be created and shared by every object of the class.
- The static variable should be declared within the class directly but outside of any method/block/constructor.
- A static variable will be stored in the method area.
- A static variable is created at the time of class loading & destroyed at the time of class unloading.So the scope of the static variable is exactly same as the scope of the .class file.
The following activities happen whenever we run the .class file for example
java Test
- start JVM
- create & start the main thread
- locate the .class file
- load the .class file
- execute .class file
- unload .class file
- terminate the main thread
- shutdown the JVM
- We can access the static variable either by object reference or by class name, but it is not recommended to use the class name.
- Within the same class, it is not required to use class name & we can access directly.
- We can access the static from both instance & static area.
example:
- static variable JVM will always provide default values.
example:-
3.local variable:-
- Sometimes we need a variable for temporary use.We can declare a variable inside a method/block/constructor such type of variable is called local variable.
- Local variable will be stored in the stack memory.
- Local variable will be created while executing the block in which we declared it.once the execution completed, automatically local variable will be destroyed.Hence the scope of local variable the block in which it declared.
example :-
- For the local variable, JVM won't provide default values.If are not using then it is not required to perform initialization.
- If we are using compulsory we should perform initialization explicitly before using that variable.Otherwise, we will get compile time errors.
example:-
No comments:
Post a Comment
Be the first to comment!
Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.
Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
Regards,
JavaByChetan
Back To Home