Java Variable
The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java.
In Java, variables must be declared before they can be used. Java allows variables to be initialized dynamically, using any expression valid at the time, the variable is declared.
Declaration
int x = 15 ; int y = Math.sqrt(a * a + b * b); // Here variable y will get the value dynamically //depending to the values of a, b.
Scope
The opening and closing of the curly braces describe scope. A scope determines what objects are visible to other parts of our program. It also determines the lifetime of those objects.
Variables are created when their scope is entered and destroyed when their scope is left.
It means variables which are declared within a method will not hold their values outside it. Also, a variable declared within a block will lose its value when the block is left.
Thus, the life-cycle of a variable is within its scope.
Here are the available scopes of variables:
- Local variables (also known as method-local variables)
- Method parameters (also known as method arguments)
- Instance variables (also known as attributes, fields, and non-static variables)
- Class variables (also known as static variables)
Local variables:- Local variables are defined within a method, constructor or block. We can not apply access modifier to local variables. Local variables are visible only within the scope of declared method, block, or constructor. The local variable should be assigned before there first use. The scope of a local variable depends on the location of its declaration within a method. The scope of local variables defined within a loop, if - else, switch statement or within a code block (marked with {} ) is limited to these curly braces. Local variables defined outside any of these constructs are accessible across the complete method.
Method parameters:- The variables that accept values in a method signature are called method parameters. They’re accessible only in the method that defines them.
Instance variables:- An instance variable is declared within a class, outside all the methods. It’s accessible to all the instance (or non-static) methods defined in a class.
Class variables:- A class variable is defined by using the static keyword. A class variable belongs to a class, not to individual objects of the class. A class variable is shared across all objects. Objects don’t have a separate copy of the class variables. We don’t even need an object to access a class variable. It can be accessed by using the name of the class in which it is defined:
Comparing the use of local variable in different scope.
- Local variables are defined within a method and are normally used to store the intermediate results of a calculation.
- Method parameters are used to pass values to a method. These values can be manipulated and may also be assigned to instance variables.
- Instance variables are used to store the state of an object. These are the values that need to be accessed by multiple methods.
- Class variables are used to store values that should be shared by all the objects of a class.