Variables in java
Based on the type of value represented by the variable all variables are divided into two types :
- Primitive variables: can be used to represent primitive values. Example: int x =10;
- Reference variables: can be used to refers to objects Example: Studen s = new Studen(), s is pointing to object.
- Instance :
- the value of the variable is varied from object to object such type of variable is called instance variables.
- for every object separate copy of the instance, a variable will be created.
- Instance variable should be declared within a class directly but outside of any method or block or constructor.
- instance variable will be created at the time of object creation and destroyed at the time of object destruction hence the scope of the instance variable is exactly the same as the scope of the object.
- The instanced variable will be stored in the heap memory as part of the object.
- we cant access the instance variable directly from the static area but we can access it by using object reference. but we can access the instance variable directly from the instance area.
- for instance, variable JVM will always provide default values and we are not required to performed initialization explicitly.
- Instance variable store is stack
- not thread safe
- static:
- if the value of the variable is not varied from the object then it is not recommended to the declared variable as instance variables we have to declare such type of the variable on class level by static keyword. in the case of an instance variable for every object, a separate copy will be created but in the case of static variables, a single copy will be created at the class level and shared by every object of the class. static variables should be declared within the class directly but outside of any method block and constructor.
- the static variable will be created at the time of class loading and destroy the time of unloading hence scope of the static variable exactly the same as the scope dot class file.
- Static variables will be stored in the method area.
- We can access static variables either by object reference or class name but recommended to use the class name. withing the same class it is not required to use the class name and we can access it directly
- Static variables are also known as class-level variables or field
- local :
- Sometimes temporary meet requirements of the programmer we declared a variable inside a method, block or constructor such type of variables calls local variables or temporary or stake variables or automatic variables.
- Local Variables will be stored inside stake memory.
- Local variables will be created while executing the block in which we declared it.
- Once block execution completed automatically local variables will be destroyed. hence the scope of the local variable is the block in which we declared it.
- For local variables that won't provide default values compulsory, we should perform initialization explicitly.
- it is not recommended to perform initialization from local variables inside logical blocks because there is no guaranty for the execution of this block always to run time.
- it is highly recommended to perform initialization for local variables at the time of declaration at least with the default value.
- the only applicable modifiers for local variables are final. by mistake, if we are trying to apply any other modifier then we will get a compile-time error.
- if we are not declaring with any modifiers then by default it is default but this rule is applicable only for instance and static variables but not for local variables
Conclusion :
- for instance and static variables, JVM will provide default value but for local variables that won't provide default values compulsory, we should perform initialization explicitly before using that variable.
- instance and static variables can be accessed by multiple threads and hence these are not thread-safe but in the case of local variables for every thread separate copies will be created and hence local variables are thread-safe.
Comments
Post a Comment