Signup/Sign In

Variable in Java

In this tutorial we will learn about Java variables, various types of variables along with code examples to understand Java variables.

What is a Variable?

When we want to store any information, we store it in an address of the computer. Instead of remembering the complex address where we have stored our information, we name that address.The naming of an address is known as variable. Variable is the name of memory location.

In other words, variable is a name which is used to store a value of any type during program execution.

To declare the variable in Java, we can use following syntax


datatype variableName;

Here, datatype refers to type of variable which can any like: int, float etc. and variableName can be any like: empId, amount, price etc.

Java Programming language defines mainly three kind of variables.

  1. Instance Variables
  2. Static Variables (Class Variables)
  3. Local Variables

Instance variables in Java

Instance variables are variables that are declare inside a class but outside any method,constructor or block. Instance variable are also variable of object commonly known as field or property. They are referred as object variable. Each object has its own copy of each variable and thus, it doesn't effect the instance variable if one object changes the value of the variable.

class Student
{
    String name;
    int age;
}
Here name and age are instance variable of Student class.

Static variables in Java

Static are class variables declared with static keyword. Static variables are initialized only once. Static variables are also used in declaring constant along with final keyword.

class Student
{
    String name;
    int age;
    static int instituteCode=1101;
}

Here instituteCode is a static variable. Each object of Student class will share instituteCode property.


Additional points on static variable:

  • static variable are also known as class variable.
  • static means to remain constant.
  • In Java, it means that it will be constant for all the instances created for that class.
  • static variable need not be called from object.
  • It is called by classname.static_variable_name

Note: A static variable can never be defined inside a method i.e it can never be a local variable.

Example:

Suppose you make 2 objects of class Student and you change the value of static variable from one object. Now when you print it from other object, it will display the changed value. This is because it was declared static i.e it is constant for every object created.

package studytonight;

class Student{
    int a;
    static int id = 35;

    void change(){

        System.out.println(id);
    }
}

public class StudyTonight {
    public static void main(String[] args) {

        Student o1 = new Student();
        Student o2 = new Student();

        o1.change();

        Student.id = 1;
        o2.change();
    }
}

35 1


Local variables in Java

Local variables are declared in method, constructor or block. Local variables are initialized when method, constructor or block start and will be destroyed once its end. Local variable reside in stack. Access modifiers are not used for local variable.

float getDiscount(int price)
{
 float discount;
 discount=price*(20/100);
 return discount;
}
Here discount is a local variable.

Variable Scope in Java

Scope of a variable decides its accessibility throughout the program. As we have seen variables are different types so they have their own scope.

Local variable: Scope of local variable is limited to the block in which it is declared. For example, a variables declared inside a function will be accessible only within this function.

Instance variable: scope of instance variable depends on the access-modifiers (public, private, default). If variable is declared as private then it is accessible within class only.

If variable is declared as public then it is accessible for all and throughout the application.

If variable is declared as default the it is accessible with in the same package.

For more details about accessibility you can refer our detailed tutorial. Click here to know more about Variable Scope

Example:

In this example, we created two variables a and i, first is declared inside the function and second is declared inside for loop. Both variables have their own scope in which they are declared, so accessing outside the block reports an error.

    
public class HelloWorld {

    public static void main(String[] args) {

        int a = 10;
        
        for(int i = 0; i<5; i++) {
            System.out.println(i);
        }
        
        System.out.println("a = "+a);
        System.out.println("i = "+i); // error

    }

}
    

error: cannot find symbol i