Types of Variables in Java – Free Java Tutorials

Variables in java

Variables are the name that is given to store certain values. It gets a memory location in a memory. Being a statically type language, declaring once the variable with certain data type cannot change to other data type. Though values or variable are changeable, it should be of the same data type. Categorization of varaible depending on the values represention can be into 2 types:

  1. Primitive Variable
    These are the variables used to store primitive data type values. For example:

    int a = 2;
    //here 'a' is primitive variable

  2. Reference Variable
    These are the variables used to represent as the object of some class. For example,

    Object o = new Object();
    //here 'o' is reference variable

Categorization of variables on the basis of the position of declaration and their behavior throughout the program into 3 types:

  1. Instance Variable

    Variables which are generally define in a class and whose values differ from object to object are an instance variable. Always then global level declaration in the class is preferance. Their default value cannot provide explicitly, rather the JVM assigns them with the default values. The scope of an instance variable is same as the scope of the object. For example

    class Employee
    {
    //declaraing variables
    String name;                           
    double salary;
    //defining constructor that stores value in variables
    Employee(String name, double salary)
    {
    this.name = name;
    this.salary = salary;
    }
    //method to print the values
    public void print(){
    System.out.println(name);
    System.out.println(salary);
    }
    class Management
    {
    public static void main(String [] args)
    {
    //creating objects
    Employee e1 = new Employee('Mark', 46000.0);
    Employee e2 = new Employee('Bill', 57000.0);
    //printing the values of the object
    e1.print();
    e2.print();
    }
    }

    Output:

    Mark
    46000.0
    Bill
    57000.0
    

    Note:- In this name and salary are instance variables as their values will be distinct regarding the objects.


  2. Static Variable

    The variables whose declaration is static are static variable. To create a static variable following syntax is as follows:

    static datatype variablename = value;

    Its creation is because its value can assign in the static block of the program. If static block tries to assign the value to a non-static variable then compile time error will generate by the compiler as “non-static variable cannot reference from a static context”. If a variable is declare as static at class level then it will have only one copy, no matter how many objects a programmer create, and its value will also share throughout all the object. These are the fields and class level variables.

    For example:

    class Employee
    {
    //declaraing variables
    String name;                           
    double salary;
    static String department;        //static variable
    //defining constructor that stores value in variables
    Employee(String name, double salary)
    {
    this.name = name;
    this.salary = salary;
    }
    //method to print the values
    public void print(){
    System.out.println(name);
    System.out.println(salary);
    System.out.println(department);
    }
    class Management
    {
    public static void main(String [] args)
    {
    //creating objects
    Employee e1 = new Employee('Mark', 46000.0);
    e1.department = "Research & Development";
    Employee e2 = new Employee('Bill', 57000.0);
    //printing the values of the object
    e1.print();
    e2.print();
    }
    }

    Output:

    Mark
    46000.0
    Research & Development
    Bill
    57000.0
    Research & Development

  3. Local Variable

    These are the variable which is declare to meet the temporary need of the program i.e. these variable may or may not fin its use throughout the program. Such variables can be define inside methods, constructor or even loop blocks. These variable are store in the stack in the memory. These have local scopes means they will create when their parent block is being in executing state and will destroy from the memory as soon as the block is complete. After their creation in the memory, they are not initialize with some default values like static variables. Local variable is not compatible with some other prefix keyword except final. Using any other keyword will generate an error in the compiler . For example

    class Local
    {
    public static void main(String [] args)
    {
    for(int i=0;i<5;i++)                               // here i is a local variable
    {
    System.out.println("Value of i is "+i);            //printing value of local variable
    }
    }
    }

    Output

    Value of i is 0
    Value of i is 1
    Value of i is 2
    Value of i is 3
    Value of i is 4

This is all about the types of variables in Java.

Leave a Comment

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