Type Here to Get Search Results !

Types Of Variables in Java

0

Types Of Variables in Java

Based On Class Scope Variables are divided in Two Types


1-Local Variables,Parameters


The Variables created inside a method or, block are called local variables .

2-Class Level Variables


The Variables created at class level are called class level variables

Two Types of Class level variables

Class level variables are divided into two types , based on the time they are getting memory location .they are

i-static variables
ii-non static variables or , instance variables

Definition-The class level variable which has static keyword in its creation statement is called static variable else it is called non static variable .

Memory Location of all above three variables-

local variables get memory location when method is called and their creation statement is executed . they get memory with respect to method , so they are also called method variables . local variables are automatically after method execution is completed so they are also called variables .

static variables get memory location when class is loaded into JVM . they get memory with respect to class name ,so they are also called class variables also called fields .

non static variables get memory location when object is created using new keyboard they get memory with respect to object so they are also called object variables or , instance variables or, properties or, attributes are also called field .

Base on the purpose and position of declaration all variables are divided into 3 types.


 1) instance variables 

2) static variables 

3) local variables 

instance variables


 if the value of a variable is varied from object to object. Such type of variables are called instance variables. For every object a separate copy of instance variables will be created. 

 Instance variables will be crated at the time of object creation and will be destroyed at the time of object destruction i.e the scope of instance variables is exactly same as the scope of object. 

We have to declare instance variables with in the class but outside of any method (or) constructor or block. 

For the instance variables no need to perform initialization. JVM will always provide values. 

static variable 


if the value of a variable is fixed for all objects then it is not recommended to declare that variable at instance level. Such type of variables we have to declare at class level by using static keyword. 

For the static variables a single copy will be created at class level and shared by all objects of that class. 

Static variables should be declared with in the class but outside of any method or block or constructor. Static variables will be created at the time of class loading and destroyed at the time of unloading. 

We can access static variables either by using class name or by using object reference using class name is recommended. 


Post a Comment

0 Comments

Recent-post