Java Variables

Java Variables

A variables is a container which holds a particular data type during the Java program execution.

There are 3 types of Variables in Java.

  1. Environment Variable
  2. Local Variable
  3. Global Variable

1. Environment Variable :-

  • We have declared this variables as installing of jdk.
  • Environment variables are part of the operating system.
  • Set path is an environment variable, which specifies the location of javac, java.
  • Path environment variable can be set by using SET.
  • Command prompt requires only one update of the set path.

2. Local Variable :-

  • This type of variable are declared as inside the method.
  • Local Variables must be initialized before the usage.
  • Local Variables are unique, we don’t declare as multiple variables with the same name.

3. Global Variable :-

  • Global variables are declared as inside the class.

Global variables are two types.

  1. Static variables
  2. Non-static variables

a). Static variables :-

  • Static variables can be accessed through class name.
  • Static variable can be loaded into the memory whenever classes is loading into the memory.
  • Static variable can be loaded only once into the memory for entire execution.

b). Non-static variables :-

  • Non-static variables can be accessed through reference variable.
  • Non-static variable can be loading into the memory while creating an object to the class.
  • Non-static variable can be loaded whenever object is created.
  • Non-static variables are object wise.
  • Non-static members are called as object members.
					public class VariableExample {

	static int i = 10; // Static variables
	int j = 20; // Non-static variables

	public static void main(String[] args) {
		int a = 30; // Local Variable
		VariableExample v1 = new VariableExample();

		System.out.println("Local variables value :- " + a);
		System.out.println("Static variables value :- " + i);
		System.out.println("NonStatic variables value :- " + v1.j);

Output :-
Local variables value :- 30
Static variables value :- 10
NonStatic variables value :- 20