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:
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.
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();}}
Line 1-8: We created class named Scopecheck
and it contains a single method, local()
.
Line 4: We declare an integer variable named local
and assigns it the value of 10
.
Line 5-6: We used the method System.out.println()
to print two messages to the console.
Line 9-14: We created class named main
class and contains a main
method, and this is the entry point of the program.
obj
of the Scopecheck
class inside the main method.local()
method of the object.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:
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:
Free Resources