What is Variable in Java


Java VariablesViews 2894

What is Variable in Java – Variables in Java are a memory location that holds a specific value. Before we use any variable, we need to declare it. Every variable is identified with a data type. We will cover data types in the next tutorial.

Variables in Java

Declaring a variable in Java

We can declare a variable in the below way.

data_type variable_name;

data_type – Refers to the type of data that the variable can hold

variable_name – the name of the variable

In the below example, we are declaring a variable of name “str” which is of data type “String“.

String str;

Assigning a value to a variable in Java

We can assign value to a variable in Java after declaring a variable or during the declaration. Both the methods are valid as you can see in the below 2 examples.

In the first example, here we declare a variable name i of data type int and then assign the value as 10 to the variable in the next line. Similarly, we can declare in the same way for String data type as well. We need to write string values within double quotes (” “).

int i;
i=10;

String str;
str = "Hello";

We can also declare and assign the variable in a single line as below

int i = 10;

String str = "Hello";

Variable Naming Conventions and Variable Name Rules in Java

While declaring any variable in java, we need to follow the below naming conventions/rules:

  • A variable name should always start with lowercase(Eg: name). If the variable name is more than 1 word, the starting letter should be lowercase and the next word starting letter should be upper case. (Eg: firstName, lastName)
  • Java does not accept variable names with spaces in them. It will be considered as invalid(Eg: first name – this is invalid)
  • Variables can begin with special characters like $ or _.
  • Variable names are case sensitive, which means Java considers “name” and “NAME” as 2 different variables.
  • We cannot use reserved words like for, int as variable names.

Variable Types in Java

There are 4 different types of Java variables:

What is Variable in Java

Static Variable in Java

When we declare a variable in Java with a keyword static, we call it as a static variable. We can also call it as a class variable. These variables are always associated with class and its value is common to any object created for the class. In other words, if we create 3 objects for a class and we change the variable value for 1 object, then the value is changed for all the objects which access that variable.

We should always declare a static variable within a class and it should be outside the method or constructor. It is not necessary that we need to access the static variables using the class instance. We can directly access it using class name as well.

In the below example, we can see that we access the static variable age directly using class name and access name using Employee object e. Also, we can directly access the static variable name alone as in the last print statement. All three statements are valid.

public class Employee {

  public static int age = 45;
  public static String name = "Adithya";
  
  public static void main(String[] args) {
    Employee e = new Employee();
    System.out.println(Employee.age);
    System.out.println(e.name);
    System.out.println(name);
  }

}
45
Adithya
Adithya

Example of static variable in Java

In the below example, we can clearly understand that static variable values are common for all the class instances.

public class Employee {

  public static int salary = 4500;
  
  public static void main(String[] args) {
    Employee e1 = new Employee();
    Employee e2 = new Employee();
    System.out.println(e1.salary);
    System.out.println(e2.salary);
    e1.salary= 5500;
    System.out.println("Value of the static variable after reassign");
    System.out.println(e1.salary);
    System.out.println(e2.salary);
  }

}
4500
4500
Value of the static variable after reassign
5500
5500

Instance Variable in Java

Instance variables are variables in Java that are associated with each instance or class object. From the word “instance” we can understand that these variable values are created for each instance of the class separately. In other words, suppose we have 3 instances, and we change the instance variable for 1 instance, this variable value remains unaffected for the remaining 2 instances.

We need to declare the instance variable within the class limit and outside method or constructor similar to static variables.

We can understand this clearly with the example given below. Since we have changed the value only for e1 object, the instance variable value for e2 remains unchanged.

Example of an Instance variable in Java

public class Employee {

  public int salary = 4500;
  
  public static void main(String[] args) {
    Employee e1 = new Employee();
    Employee e2 = new Employee();
    System.out.println(e1.salary);
    System.out.println(e2.salary);
    e1.salary = 5500;
    System.out.println("Value of the instance variable after reassign");
    System.out.println(e1.salary);
    System.out.println(e2.salary);
    
  }

}
4500
4500
Value of the instance variable after reassign
5500
4500

Local variable in Java

When we declare a variable in Java within a method, we call it as a local variable. In other words, it is local to the method and we cannot change the value or access it outside the method limit.

Example of local variable

public class VariableDemo {

  public void displayNumber() {
    int number = 100;
    System.out.println("Value of number is: " + number);
  }
  public static void main(String[] args) {
    VariableDemo vd = new VariableDemo();
    vd.displayNumber();

  }

}
Value of number is: 100

Suppose, we try to access the local variable outside, it will throw compilation error as in the below example. This clearly shows that we can access local variables only in the method where it is declared.

public class VariableDemo {

  public void displayNumber() {
    int number = 100;
    System.out.println("Value of number is: " + number);
  }
  public static void main(String[] args) {
    VariableDemo vd = new VariableDemo();
    System.out.println(number);
  }

}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
  number cannot be resolved to a variable

  at VariableDemo.main(VariableDemo.java:11)

Final Variable in Java

When we declare a variable with a keyword final, we call it a final variable. This means that we cannot change the value of the variable. If we try to modify the value of a final variable, it will throw compilation error.

public class VariableDemo {

  public static void main(String[] args) {
    final int a = 5;
    a = 3;
    System.out.println(a);		
  }
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
  The final local variable a cannot be assigned. It must be blank and not using a compound assignment

  at VariableDemo.main(VariableDemo.java:6)

Example Java Program to illustrate Variable types

In the below example, we can clearly understand how we differentiate between the variable types in Java.

public class Employee {

  //Static variable
  public static String role = "Developer";
  
  //Instance variable
  public int salary = 4000;
  
  public void displayName() {
    //Local variable
    String name = "Arthi";
    System.out.println("Employee Name: " + name);
  }
  
  public static void main(String[] args) {
    Employee e1 = new Employee();
    e1.displayName();
    System.out.println("Employee role: " + role);
    System.out.println("Employee Salary: " + e1.salary);
    
  }

}
Employee Name: Arthi
Employee role: Developer
Employee Salary: 4000

Assigning one variable value to another variable in Java

We can copy the value of one variable to another variable by simple assignment operator “=” as given in the below example. Here, we are copying the value of “a” variable to “b” variable. We can also reassign values to a variable as long it is not declared as final.

public class CopyVariable {

  public static void main(String[] args) {
    int a = 10;
    int b;
    System.out.println("Value of a:" + a);
    b=a;
    System.out.println("Value of b:" + b);
    a = a + 5;
    System.out.println("New value of a:" + a);
  }

}
Value of a:10
Value of b:10
Value of a:15

Reference

Translate »