Introduction:
•A variable is a basic unit of storage in a Java program.
•Generally a variable is defined as a placeholder in the memory for holding a certain value.
•As the name implies the value held by the variable can vary over the execution of the program.
Syntax for declaring a variable:
type identifier [ = value];
Example:
int a; //Here ‘a’ is the variable name.
Initialization of a Variable:
•Initialization means simply assigning a value to the variable.
Example:
int a = 10;
So, the variable ‘a’ stores the value 10. This kind of initialization is also called as “static” or “compile-time initialization”.
•There is another kind of initialization known as “dynamic initialization” or “run-time initialization”. It as shown below:
Example:
int a = Integer.parseInt(JOptionPane.showInputDialog(“Enter the value of a: “));
In the above piece of code we are trying to initialize the value of variable ‘a’ with the value being read from the keyboard during the execution of the program.
•There is another kind of dynamic initialization. It is as follows:
int a = 10;
int b;
b = a*2;
In the above piece of code the variable ‘b’ is initialized during the execution of the program with the value 20.
Scope and Lifetime of Variables:
•Scope determines the visibility of the variables, means in which parts of the program can we access the variables.
•Scope also specifies the life-time of a variable.
•For now, Lets see the scope of variables declared inside methods(functions) and blocks.
•A block is a set of statements enclosed with in braces like:
{
statements;
}
•A variable’s scope is within the block in which it is declared.
Example:
{
int x;
System.out.print(x);
}
System.out.print(x);
In the above piece of code first print statement is valid because we are accessing the variable ‘x’ within the block in which it is declared. But, the second print statement gives an error as it is trying to access the variable ‘x’ outside of the block in which it is declared.
•Scope of variables in nested blocks. A nested block means, a block within another block as shown below:
Example:
{
int x;
{
int y;
System.out.print(x);
}
System.out.print(y);
}
•The scope of variables in nested blocks is like, the variables declared in the outer-block are visible in the inner-block. But, the variables declared inside the inner-block are not visible to the outer-block.
•In the piece code shown above, the variable ‘x’ declared inside the outer-block can be accessed inside the inner-block.
•But, the variable ‘y’ which is declared inside the inner-block cannot be accessed inside the outer-block. So, the second print statement gives an error.
Types of Variables:
•In Java there are four types of variables:
1) Instance variables.
2) Class variables.
3) Local variables.
4) Parameters.
Local variables are the variables which are declared inside a block. Parameters are also a kind of local variables.
So, until now we have seen the scope of only local variables and parameters.
•A variable is a basic unit of storage in a Java program.
•Generally a variable is defined as a placeholder in the memory for holding a certain value.
•As the name implies the value held by the variable can vary over the execution of the program.
Syntax for declaring a variable:
type identifier [ = value];
Example:
int a; //Here ‘a’ is the variable name.
Initialization of a Variable:
•Initialization means simply assigning a value to the variable.
Example:
int a = 10;
So, the variable ‘a’ stores the value 10. This kind of initialization is also called as “static” or “compile-time initialization”.
•There is another kind of initialization known as “dynamic initialization” or “run-time initialization”. It as shown below:
Example:
int a = Integer.parseInt(JOptionPane.showInputDialog(“Enter the value of a: “));
In the above piece of code we are trying to initialize the value of variable ‘a’ with the value being read from the keyboard during the execution of the program.
•There is another kind of dynamic initialization. It is as follows:
int a = 10;
int b;
b = a*2;
In the above piece of code the variable ‘b’ is initialized during the execution of the program with the value 20.
Scope and Lifetime of Variables:
•Scope determines the visibility of the variables, means in which parts of the program can we access the variables.
•Scope also specifies the life-time of a variable.
•For now, Lets see the scope of variables declared inside methods(functions) and blocks.
•A block is a set of statements enclosed with in braces like:
{
statements;
}
•A variable’s scope is within the block in which it is declared.
Example:
{
int x;
System.out.print(x);
}
System.out.print(x);
In the above piece of code first print statement is valid because we are accessing the variable ‘x’ within the block in which it is declared. But, the second print statement gives an error as it is trying to access the variable ‘x’ outside of the block in which it is declared.
•Scope of variables in nested blocks. A nested block means, a block within another block as shown below:
Example:
{
int x;
{
int y;
System.out.print(x);
}
System.out.print(y);
}
•The scope of variables in nested blocks is like, the variables declared in the outer-block are visible in the inner-block. But, the variables declared inside the inner-block are not visible to the outer-block.
•In the piece code shown above, the variable ‘x’ declared inside the outer-block can be accessed inside the inner-block.
•But, the variable ‘y’ which is declared inside the inner-block cannot be accessed inside the outer-block. So, the second print statement gives an error.
Types of Variables:
•In Java there are four types of variables:
1) Instance variables.
2) Class variables.
3) Local variables.
4) Parameters.
Local variables are the variables which are declared inside a block. Parameters are also a kind of local variables.
So, until now we have seen the scope of only local variables and parameters.
Thank you for sharing Amazing Blog. It's providing very useful guideline for Engineering students.
ReplyDeleteget more: <a href="https://ekeeda.com/subject/object-oriented-programming-through-java>Object Oriented Programming Through Java</a>