Variables in java

In this section, we are going to discuss about the variables in java programming language.

You can also refer our video tutorial for same.

So let’s start with this tutorial :

Variables in java

  • A variable is basically a container which can store the value which is used by the java program during its execution.
  • Variable can hold the values ranging from texts to numbers, temporary results of multi step calculations etc.
  • A variable is a name given to the memory location or we can say that it is name of memory location.

The value of variable can be changed as its name suggest (variable= vary + able, that means which is able to vary or change).

Declaration of variable:- 

Declaration of Variable
Data-type variable-name = value;

Data-type: Data-type specifies the different size and values that can be stored in a variable.
Variable-name: It is the name given to the variable.
Value: It is the initial value stored in the variable.

Example :-

Type of variables:- 

There are three type of variables in java:

1. Local Variable

  • They are declared inside the method and get no default value.
  • It is mandatory to initialize a local variable before use because they are blank.
  • They cannot access outside the method in which they are declared.

Example :

class VarDemo
{
   void display ()
   {
        int a=100; // local variable
        System.out.println(a);
   }
   public static void main (String[] arg)
   {
        VarDemo v=new VarDemo();
        v.display();
   }
}

Here a is a local variable and we can access it outside the method display().

Output
100

2. Instance Variable

  • They are declared inside the class but outside the method.
  • Unlike the local variable , they get default value.
  • Instance variable is a part of object because they live inside an object. Instance variables are created when an object is created.
  • JVM create one copy of instance variable per object.
  • Instance variable is also called as the non-static variable.
  • Non-static variable is access using the object name.

Example :

class VarDemo {
    String name; // instance variable
    VarDemo(String n){
    name=n;
}
   Void display(double salary){
       System.out.println(“name is: “+name+”\n salary is :”+salary);
   }
   public static void main (String[] arg){
       VarDemo v=new VarDemo(“ram”);
       v.display(2000);
   }
}           

Here name is the instance variable because it is declared inside the class not a method. As from the above example, we can see that we need not to initialize our instance variable with a default value unlike the local variable.

Output
name is :ram
salary is :2000

3. Static variable

  • They are declared inside the class using static keyword.
  • They get default values.
  • JVM create one copy of static variable. This copy will be shared by all the objects .
  • Basically static variable is used to store the common data among the objects.
  • Static variable can be access by class name, we do not need to create the object to access static variable

Example :

class VarDemo{
    static String place=”Delhi”; //static variable
    display(String n, int a){
    System.out.println(“name is :”+n+”\n age is:”+a+”\n place is :”+place);
}
    public static void main (String[] arg) {
         VarDemo v=new VarDemo();
         VarDemo v1=new VarDemo();
         v.display(”krish”,10);
         v1.display(”smith”,20);
    }
 }

Here you can see that place is static variable and this static variable will be shared with both objects v and v1 of class VarDemo.

Output
name is :krish
age is :10
place is :Delhi
name is :smith
age is :20
place is :Delhi

If you found this post useful, don’t forget to share this with your friends, and if you have any query feel free to comment it in the comment section.

Thank you 🙂 Keep Learning !

Leave a Comment

Your email address will not be published. Required fields are marked *