Variable Types in Java

Introduction

A variable specifies a memory location that contains data. The data can be numbers, characters and so on. There are three types of variables in Java:

  1. Local variables
     
  2. Instance variables
     
  3. Class variables

Local variables

  • A method uses local variables for storing its temporary state.
     
  • Generally these are declared inside the methods, constructors or blocks of code.
     
  • These should always be declared and initialized before their use.
     
  • We cannot access local variables ahywhere other than the specific methods, constructors or blocks of code in which they are declared.
     
  • When the method is executed completely, these local variables are destroyed.

Example:

package myclass1;

class Myclass1

{

   public void classStrength()

   {

      int strength = 100;

      strength = strength - 10;

      System.out.println("Class strength is : " + strength);

   }

   public static void main(String args[])

   {

      Myclass1 count = new Myclass1();

      count.classStrength();

   }

}

 

Output:


local variable.jpg

 

Instance variables

  • These are also known as non-static variables.
     
  • These are declared inside a class, but outside a method, constructor or a block of code.
     
  • Instance variables have a default value.
     
  • These are created and destroyed when the object is created and destroyed respectively.
     
  • Inside the class these can be accessed directly by calling their names.
     
  • Instance variables can be accessed by all methods, constructors or blocks of code in the same class.

Example:

package myclass1;

class Myclass1

{

    public String location;

    private int id;

    public Myclass1(String Location)

    {

        location = Location;

    }

    public void id(int classid)

    {

        id = classid;

    }

    public void Location()

    {

        System.out.println("Location : " + location);

        System.out.println("id :" + id);

    }

    public static void main(String args[])

    {

        Myclass1 scl = new Myclass1("ndl");

        scl.id(10090);

        scl.Location();

    }

}

Output:

instance variable.jpg

Class variables

  • These are also known as static variables.
     
  • These are declared inside a class with a static keyword, but outside a method, constructor or a block of code.
     
  • These variables are created and destroyed when the program starts and ends respectively.
     
  • Class variables also have a default value.
     
  • These variables can be accessed just by referring to them with the class name.

Example:

package myclass1;

class Myclass1

{

    private static int rollno;

    public static final String CLASS = "class topper";

    public static void main(String args[])

    {

        rollno = 10;

        System.out.println(CLASS+ " roll no is :" + rollno);

   }

}

Output:

class variable.jpg

Summary

This article will introduce you to the various types of variables in Java and their properties.

Up Next
    Ebook Download
    View all
    Learn
    View all