Variable in Java programming5 min read

Introduction

A variable is a data item whose value varies during program execution. A variable in Java can be termed as a basic unit of storage. Variables give us named storage that we can manipulate with our programs. Each variable in Java has a specific type, that governs the memory size and layout, the range of values that may be stored within that memory, and the set of operations that may be applied to the variable. Every variable has a scope that defines the visibility and lifetime of that variable. Every variable has a data type. We can initialize a variable in java at the time of declaration but it is optional.

Examples:

  1. int rollno;
  2. int total = 100;
  3. byte a= 50;
  4. char sex = ‘M’;
  5. double pi = 3.14285714285;
  6. double percentage = 89.66;
  • As per the rule, a variable declared inside the scope is not visible/accessible outside the scope
  • The scope can be nested.
  • A variable in java can also be declared dynamically
  • A variable can be declared anywhere in the block. But it will be available for the statements following by declaration statement in the same block
  • When one type of variable in java is assigned to another type of variable, implicit type conversion takes place if the types are compatible and the destination type is larger than the source type.
  • The implicit type conversion is not possible then explicit type casting is required.
different types of variable in java

Different types of Variables in java

There are three kinds of variables in Java

  1. Local variables – these are declared in methods of blocks and are instantiated for each invocation of the method of the block.
  2. Instance variables – these are members of a class and instantiated for every object of the class.
  3. Class/static variables – these also are a member of class i.e. they belong to class but are not instantiated for any object of the class.

Local Variable

  • In methods, constructors, and blocks, local variables are defined.
  • When a method, function, or block is entered, a local variable is created, and the variable is removed when the method, function, or block is exited.
  • For local variables, access modifiers aren’t allowed.
  • Only the declared method, function, or block can see local variables.
  • Internally, local variables are implemented at the stack level.
  • Because local variables have no default value, they must be declared and an initial value assigned before they are used for the first time.

Example Local Variable :
Here, age is a local variable. This is defined inside pupAge() method and its scope is limited to this method only.

public class Test {
	public void pupAge(){
		int age = 0;
		age = age + 7;
		System.out.println("Puppy age is: " + age);
	}
	public static void main(String args[]){
		Test test = new Test();
		test.pupAge();
	}
}

This would produce the following result:

Puppy age is: 7

Instance Variable

  • Instance variables are declared in a class, but outside a method, constructor, or block.
  • When space in the heap is allocated for an object, a slot is created for each instance variable value.
  • When an object is created using the keyword ‘new,’ instance variables are generated, and they are destroyed when the object is destroyed.
  • Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object’s state that must be present throughout the class.
  • Instance variables can be declared at a class level before or after use.
  • Access modifiers can be given for instance variables.

Example of Instance Variable :
Here, name and salary are instance variables.

import java.io.*;
public class Employee {
  
	// this instance variable is visible for any child class.
	public String name;
  
	// salary variable is visible in Employee class only.
	private double salary;
  
	// The name variable is assigned in the constructor.
	public Employee (String empName){
		name = empName;
	}
  
	// The salary variable is assigned a value.
	public void setSalary(double empSal){
		salary = empSal;
	}
  
	// This method prints the employee details.
	public void printEmp(){
		System.out.println("name: " + name );
		System.out.println("salary: " + salary);
	}
  
	public static void main(String args[]){
		Employee empOne = new Employee(Rasika);
		empOne.setSalary(1000);
		empOne.printEmp();
	}
}

This would produce the following result:

name: Rasika
salary: 1000.0

Class Variable

  • As you’ve learned, class variables are variables that are specified and saved within the class. Their values are applied to the entire class as well as all of its instances.
  • The values of the instance variables can then be changed by each instance independently of the other instances.
  • Only one copy of a variable exists with class variables. When you alter the value of a variable, it affects all instances of that class.
  • The static keyword comes before the variable name when defining class variables. Example: static int age;
    • These variables are referred to as static variables since one of the definitions of the word static is “fixed in one place.” If a class has a static variable, that variable has the same value in all of its objects.
  • Class variables are accessed using the same dot notation as instance variables. You can use the instance or the name of the class on the left side of the dot to obtain or alter the value of the class variable.
  • You may alter the value of a class variable using an instance, it’s easy to be confused about class variables and where their values come from. Keep in mind that the value of a class variable has an impact on all of its instances. As a result, when referring to a class variable, it’s best to use the name of the class. It simplifies the reading of your code and the debugging of odd results.

Example of Class Variable:

import java.io.*;
public class Employee {
  
	// salary variable is a private static variable
	private static double salary;
  
	// DEPARTMENT is a constant
	public static final String DEPARTMENT = "Development" ;
  
	public static void main(String args[]){
		salary = 1000;
		System.out.println(DEPARTMENT + " average salary: " + salary);
	}
}

This would produce the following result:

Development average salary: 1000

Share:

Leave a Reply