In programming, the scope of a 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. The scope of a variable is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e.scope of a variable can determined at compile time and independent of the function call stack
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. Variables declared inside a block or method are not visible 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
Check out the Entri App for more details on Java programming!
Types of Variable Scope in 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 and have class-level scope. We can access these variables anywhere inside the class.
Syntax:
public class DemoClass
{
//variables declared inside the class have class-level scope
int age;
private String name;
void displayName()
{
//statements
}
int dispalyAge()
{
//statements
}
char c;
}
Local Variables (Method Level Scope)
These are the variables that are declared inside a method, constructor, or block that 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
public class DemoClass1
{
void show()
{
//variable declared inside a method has method level scope
int x=10;
System.out.println(“The value of x is: “+x);
}
public static void main(String args[])
{
DemoClass1 dc = new DemoClass1();
dc.show();
}
}
Learn to code from industry experts! Enroll here!
Both accessibility and visibility play a significant role in programming because of the concept of scope. Any single mistake in scoping the variable can lead a programmer to a compile-time error. The scope of a static variable resides within the class only. The scope defines the region within a program from where the variables will be accessible. In general, a set of curly brackets { } defines a scope. In Java, we can usually access a variable as long as it was defined within the same set of brackets as the code we are writing or within any curly brackets inside of the curly brackets where the variable was defined.
Entri provides video classes as well on various important topics by the excellent faculties. One will get revision modules, and monthly tests based on the classes. Entri provides an excellent platform with full-length mock tests including previous year’s question papers. It also gives you access to clarify your doubts.