Variables In JAVA


Variables in java -

Variables is like a container which hold the values, when our JAVA program is execute.

Variable is the name of memory location(reserved area allocated in memory) where we store the value.

We can change the value of variable according to our requirement(it can  be int, float, boolean, char). 

Types of variable-

   1.Local variable

   2.Instance variable

   3.Static variable

     EXAMPLE-

          class Var{

             int data=500;// instance variable

             static int num=1000;//static variable

             void method(){

             int amount=2000;//local variable

               }

               }  //end of the class                   

1.Local variable-

Declaration- Local variables declares inside the methods, constructors, blocks.

Scope- Inside the methods, constructors, blocks, not outside.

When variable gets allocated- When method, constructor, block get executed.

Stored memory- Always allocate in stack memory.

Default values- Doesn't have any default values.

Access modifier-Can not be used with local variable.

2.Instance variable-

Declaration- Instance variables declares inside the class but outside the methods, constructors or blocks. 

Scope- Inside the methods, constructors, blocks or within a class we can use.

When variable gets allocated- When object is created variables allocated.

Stored memory- Always allocate in heap memory.

Default values- They have default values (int=0; boolean=false; object=null).

Access modifier-Can  be used with instance variable.

3.Static variable-

Declaration- Static variables declares with static keywords inside the class but outside the methods, constructors or blocks.

Scope- Inside the methods, constructors, blocks or within a class we can use.
When variable gets allocated-When we run program and dot class file loaded variable is allocated.

Stored memory- Static memory.

Default values- They have default values (int=0; boolean=false; object=null).

      







Comments