Variables in JAVA programming Hands on coding

1. What is Variable ?

  • Variable is the name of storage space which can be used to store data.
  • The value of the variable may change. It contains last value which is stored into it.
  • Variable should be always declared with data type.

2. Variable Declaration
 int num;   
 float height;   
 char name;   

  • Here num is a variable of  int type.
  • height is a variable of float type.
  • name is a variable of type char.


3. Variable Initialization

You can initialize the variable at the time of declaration of variable only or you can initialize after the  declaration.

 int registration_num=201;   
 float height=5.6;   
 char division='A';   


  • 201 is the value of registration_num. 
  • 5.6 is the value of height. 
  • A is the division. 


Note; Character value is always written in single quotes.


Let's see another method of variable Initialization

You can initialize the variable after the declaration of variable.


int register_num; 

float height; 

char division; 


registration_num=201; 

height=85.6; 

division='A'; 


Rules to create the variable;

  • First letter of any variable must be either alphabet or underscore(_).
  • The variable name must not begin with digits.
  • Next to the first character it may contain combination of alphabets or digits.
  • White spaces are not allowed.
  • Variable name must not be a keyword.


Types of Variable

  1. Local Variable
  2. Instance Variable
  3. Static Variable


1. Local Variable

  • The variable declared within the body of the method or constructor or block can be called as local variable.
  • You can use local variable only inside that function in which it will be declared and it can be a static variable.


 class Example   
 {   
  public void add()   
   {   
    int x,y=15,z=25;  //declaring local variable   
    x=y+z;   
    System.out.println("Add="+x);   
   }   
   public static void main(String[] args)   
   {  
     Example obj=new Example();   
     obj.add();   
   }   
 }   

/ ### Output ### 

Add=40 / 


Instance Variable

  • This variable can be declared inside the class but outside the body of the constructor/block is called instance variable.
  • You can use instance variable 
  • anywhere in the program.


 class Easy   
 {   
   //these instance variabe can be used   
  //inside all the function of this class   
   int x,y=30,z=20;   
   public void add()   
   {   
     x=y+z; //accessing instance variable   
    System.out.println("Add="+x);   
   }   
   public void sub()   
   {  
    x=y-z;   //accessing instance variable   
   System.out.println("Sub="+x);   
   }   
   public static void main(String[] args)   
   {   
    Easy obj=new Easy();   
    obj.add();   
    obj.sub();   
   }   
 }   

/ ### Output ### Add=30 Sub=10 / 


Static Variable

  • This is the variable which will be declared with static keyword, inside a class but outside the body of the method or constructor or block.
  • Static variable will be stored in the static memory.
  • Static variables will be created when the program starts and it will be destroyed when the program stops.
  • You can call static variable by the name of class directly.


 class Easy   
 {  
   //declaring static variable   
   static String text="You are a smart guy.";   
   public static void main(String[] args)   
   {   
      System.out.println(Easy.text);   
   }   
 }   


/ ### Output ### 

You are a smart guy. /


This is regarding variables in JAVA. 

 For Videos Join Our Youtube Channel: Join Now

Thank you