What is the scope of a variable in Java?
The scope of a variable refers to the portion of a program where the variable can be accessed. The scope of a variable can be determined by its location in the source code, as well as by any blocks of code (such as loops or conditional statements) that enclose it.
There are three main types of variable scope in Java:
- Local scope
- Instance scope
- Static scope
The above illustration explains the three types of scope we mentioned above and is the visual point of view of the variable scope. Let’s move forward with the variable scope and get a better understanding through explanation and code.
Local scope
This refers to variables that are defined within a method or block of code. These variables are only accessible within the method or block where they were declared, and they are not visible to any code outside of that scope.
class Scopecheck{public void local(){int local = 10;System.out.println( "'local' can be accessed only inside this function");System.out.println( "Scope of local: " + local);}}public class main {public static void main(String[] args) {Scopecheck obj = new Scopecheck();obj.local();}}
Explanation
-
Line 1-8: We created class named
Scopecheckand it contains a single method,local().-
Line 4: We declare an integer variable named
localand assigns it the value of10. -
Line 5-6: We used the method
System.out.println()to print two messages to the console.
-
-
Line 9-14: We created class named
mainclass and contains amainmethod, and this is the entry point of the program.- Line 11: We created an object named
objof theScopecheckclass inside the main method. - Line 12: We called
local()method of the object.
- Line 11: We created an object named
Instance scope
This refers to variables that are defined as fields of a class (also known as instance variables). These variables are accessible within the entire class, including any methods, constructors, and nested classes.
class Scopecheck{int instance = 20;public void inst(){int local = 10;System.out.println( "'local' can be accessed only inside this function");System.out.println( "Scope of local: " + local);System.out.println( "'instance' can be accessed throught the class with normal functions");System.out.println( "Scope of instance: " + instance);}}public class main {public static void main(String[] args) {Scopecheck obj = new Scopecheck();obj.instance = 50;obj.inst();Scopecheck obj2 = new Scopecheck();obj2.inst();}}
In the program above:
- We call the instance variable and from lines 14-18.
- When we call an instance variable for the first time, its value changes.
- When another call is made at line 17, its value returns to the previous one as it can be changed only for one instance.
Static scope
This refers to variables that are defined as static fields of a class. These variables are also accessible within the entire class, including any methods, constructors, and nested classes. Although they are shared among all instances of the class; these variables are accessible from anywhere in the code, even outside the class.
class Scopecheck{static int stat = 20;public void stat(){int local = 10;System.out.println( "'local' can be accessed only inside this function");System.out.println( "Scope of local: " + local);System.out.println( "'static' can be accessed throught the class and other classes");System.out.println( "Scope of static: " + stat);}}public class main {public static void main(String[] args) {Scopecheck obj = new Scopecheck();Scopecheck.stat = 60;obj.stat();Scopecheck obj2 = new Scopecheck();obj2.stat();}}
In the program above:
- The value of the static variable, unlike the instance variable, changes for all the instances.
- Lines 16 and 18 return the same value of the static variable as it will remain the same until we change it.
Free Resources