Java – Variable

  • Post category:Java
  • Reading time:7 mins read

Java variables are piece of memory that can store data values.

A variable is assigned with a data type. Variables are generally used to store the information a Java program needs to do its job. The value stored in the variable can be changed while the program is running.

In Java, all variables must be declared before they can be used. A variable is simply a name that is assigned to a memory location, which are all operations performed on the effect of a variable on that memory location.

variable_reserved

How to declare and initialize a variable ?

variable

Datatype is specifies the size and type of values ​​that can be stored in variable.

Types of Variables

There are three types of variables in Java.

  • local variable
  • instance variable
  • static variable
variable_types

Local Variable

Variables declared within the body of a method or constructor are called local variables. This variable can only be used within its methods and constructors, and other methods in the class are unaware of the variable’s existence.Local variables cannot be defined with the keyword “static”.

you can access these variables only within that block or method in which the variable is declared.

Example

public class Hospital {
	public void patientDetail() {
		// Local variable
		int patientAge = 10;
		patientAge = patientAge + 5;
		System.out.println("patient age is : " + patientAge);
	}

	public static void main(String args[]) {
		Hospital obj = new Hospital();
		obj.patientDetail();
	}
}

Output

patient age is : 15

In the above program, the variable patientAge is a local variable to the method patientDetail(). If we use the variable age outside patientDetail() method, the compiler will produce an error .

Instance Variable

A variable declared within the class but outside the body of the method, constructor or block, is called an instance variable. It is not declared static.

It is called an instance variable because its value is specific to the instance and is not shared between other instances.
Because instance variables are declared in a class, these variables are created when an object is created in the class and is destroyed when the object is destroyed. The instance variable can only be accessed by creating objects.

Unlike local variables, we can use access identifiers for instance variables. If we do not specify an access identifier, the default access specifier will be used. The initialization of the instance variable is not mandatory. Its default value is 0.

Example

public class ColorValueDemo {
	public static void main(String args[]) { 
		// first object
		Color obj1 = new Color();
		obj1.redValue = 90;
		obj1.pinkValue = 95;
		obj1.yellowValue = 98;
		
		// second object
		Color obj2 = new Color();
		obj2.redValue = 80;
		obj2.pinkValue = 60;
		obj2.yellowValue = 85;

		System.out.println("Value for first object:");
		System.out.println(obj1.redValue);
		System.out.println(obj1.pinkValue);
		System.out.println(obj1.yellowValue);

		System.out.println("Value for second object:");
		System.out.println(obj2.redValue);
		System.out.println(obj2.pinkValue);
		System.out.println(obj2.yellowValue);
	}
}

class Color {
	// Below variables are instance variables.
	int redValue;
	int pinkValue;
	int yellowValue;
}

Output

Value for first object:
90
95
98
Value for second object:
80
60
85

In the above program redValue,pinkValue,yellowValue in class colours are instance variables. We have created two object of colours class and we have assigned different values , each object will have its own copies of instance variables.

Static variable

A variable declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it among all instances of the class. Memory allocation for the static variable occurs only once when the class is loaded into memory.

Static variables are also known as class variables. Static variables are created at the beginning of program execution and are automatically destroyed at the end of execution.
These variables are declared similarly to instance variables, the difference is that static variables are declared using the static keyword inside a class outside any constructor or method block. Initialization of the static variable is not mandatory. Its default value is 0.

To access the static variables, it is not necessary to create an object of that class, we can simply access the variable using className.variableName .

Example

class Customer {
	// age,name is static variable
	public static int age;
	public static String name = "Amit";
}

public class CustomerDemo {
	public static void main(String args[]) {
		Customer.age = 25; // assign value without creating object of class
		System.out.println("Customer name is: " + Customer.name);
		System.out.println("Customer age is :" + Customer.age);
	}
}

Output

Customer name is: Amit
Customer age is :25