Scope of Variables in Java
In programming, scope of variable defines how a specific variable is accessible within the program or across classes. In this section, we will discuss the scope of variables in Java.
Scope of a Variable
In programming, a variable can be declared and defined inside a class, method, or block. It defines the scope of the variable i.e. the visibility or accessibility of a variable. Variable declared inside a block or method are not visible to outside. If we try to do so, we will get a compilation error. Note that the scope of a variable can be nested.
- We can declare variables anywhere in the program but it has limited scope.
- A variable can be a parameter of a method or constructor.
- A variable can be defined and declared inside the body of a method and constructor.
- It can also be defined inside blocks and loops.
- Variable declared inside main() function cannot be accessed outside the main() function
Demo.java
In Java, there are three types of variables based on their scope:
- Member Variables (Class Level Scope)
- Local Variables (Method Level Scope)
Member Variables (Class Level Scope)
These are the variables that are declared inside the class but outside any function have class-level scope. We can access these variables anywhere inside the class. Note that the access specifier of a member variable does not affect the scope within the class. Java allows us to access member variables outside the class with the following rules:
Access Modifier | Package | Subclass | Word |
---|---|---|---|
public | Yes | Yes | Yes |
protected | Yes | Yes | No |
private | No | No | No |
default | Yes | No | No |
Syntax:
Let’s see an example.
VariableScopeExample1.java
Output:
We see that y=100 is unknown. If you want to compile and run the above program remove or comment the statement y=100. After removing the statement, the above program runs successfully and shows the following output.
Sum of x+y = 30
There is another variable named an instance variable. These are declared inside a class but outside any method, constructor, or block. When an instance variable is declared using the keyword static is known as a static variable. Their scope is class level but visible to the method, constructor, or block that is defined inside the class.
Let’s see an example.
Product.java
Output:
Product Name: Mac Book Product Price: 65000.0
Let’s see another example.
StaticVariableScope.java
Output:
The value of PI is: 3.14159265359
Local Variables (Method Level Scope)
These are the variables that are declared inside a method, constructor, or block have a method-level or block-level scope and cannot be accessed outside in which it is defined. Variables declared inside a pair of curly braces {} have block-level scope.
Declaring a Variable Inside a Method
Output:
The value of x is: 10
Let’s see another example of method-level scope.
DemoClass2.java
Output:
The value of a is: 3
In the above example, we have passed a variable as a parameter. We have used this keyword that differentiates the class variable and local variable.
Declaring a Variable Inside a Constructor
VariableInsideConstructor.java
Output:
Age is: 24
Declaring a Variable Inside a Block
VariableInsideBlock.java
Output:
We see that y=100 is unknown. If you want to compile and run the above program remove or comment the statement y=100. After removing the statement, the above program runs successfully and shows the following output.
Sum of x+y = 30
Let’s see another example.
BlockScopeExample1.java
Output:
When we run the above program, it shows an error at line 9, cannot find symbol because we have tried to print the variable x that is declared inside the loop. To resolve this error, we need to declare the variable x just before the for loop.
BlockScopeExample2.java
Output:
012345678910