Types of Variables in JAVA Programming Language

Types of Variables in JAVA

Now we Learn Types of Variables in Java, But remember the last topic we learned ‘the data-types’ the main reason to use data-types was to make sure to choose the keyword that can signify the type of data that is being used.

But wait, we are telling the machine that we are going to use the type of data by simply writing the type like String, int, float, etc.

But the question arises here, where will be the value stored that will be given by us. From here the concept of variables comes

Variables are the user given characters or words that hold the value given by you. In other words, it can be called the name of the reserved area that is allocated in the memory. For better understanding, the term variable can be divided into two parts “vary + able” which means the value can be changed.

In Java there are three Types of Variables:

Types of Variables in JAVA

Local Variable:-

These variables are declared inside the method body. These variables can be used or accessed inside that particular method body and the other methods will not be able to know about it. It never exists for them. These variables do not have a default value so make sure the variable you declared must be initialized (given value) within that method.

For Example- INPUT:- 

public class Studetail


    public void StuAge() 

    {   //local variable age

        int age = 0;

        age = age + 7;

        System.out.println(“Student age is : ” + age);


  public static void main(String args[])


        Studetail obj = new Studetail ();


    } }

Output:-  Student age is :  7

Instance Variables:-

These variables are declared outside the methods and inside the class. These variables can be used within the entire program. The instance variables cannot be declared static.

For Example- INPUT:- 

class Cybermarquee {

    int data = 77; //instance variable 

    static int a = 2; //static variable 

    void method() {

        int b = 70; //local variable 





Static Variables:-

The variables which are declared as static are a static variable. These variables are also called the class variables because these variables can be excess using the class name.

For Example- INPUT:- 


class Demo {

  // static variable salary

   public static double salary;

   public static String name = “Manish”;


public class Demostudent


     public static void main(String args[]) {

   //accessing static variable without object         

      Demo.salary = 5000;

      System.out.println( + “average salary:” + Demo.salary);


Output:- Manish average salary: 5000.0

Rules to Declare a Variables

Types of Variables

  • The variable name should start with alphabet followed by the numbers (e.g. – name, class, book, etc.)
  • No special characters are allowed except the dollar sign ‘$’.
  • In JAVA underscore ‘_’ is considered a character. So we can use underscore to create a name of the variable.
  • The variable name should not be same as the keyword (Data-type) name, if you want it so then remember to capitalize any of the letters so it may not look like a keyword (e.g.- ‘float’ is a keyword if you want to float to be your variable too then use ‘Float’). In this example float is a data-type and even though we used Float as a variable name too. How it got possible? The answer to this question is JAVA is a case-sensitive language so ‘f’ and ‘F’ are different so JVM (Java Virtual Machine) gives the permission to use.

Types of Variables in JAVA

Rohit Verma
You Know Who I am

Leave a Reply