The scope of a variable is the accessible part of the variable in the program. Like C/C++, in Java, all identifiers are lexical (or static) scope, that is, the scope of the variable can be determined at compile time and is independent of the function call stack. Java programs are organized in the form of classes. Each class is part of a package. Java scope rules can be included in the following categories.
Member Variables (Class Level Scope)
These variables must be declared inside class (outside any function). they will be directly accessed anywhere in school . Let’s take a glance at an example:
public class Test { // All variables defined directly inside a class // are member variables int a; private String b; void method1() {....} int method2() {....} char c; }
Modifier Package Subclass World public Yes Yes Yes protected Yes Yes No Default (no modifier) Yes No No private No No No
Local Variables (Method Level Scope)
public class Test { void method1() { // Local variable (Method level scope) int x; } }
Note : Local variables don’t exist after method’s execution is over.
class Test { private int x; public void setX(int x) { this.x = x; } }
The above code uses this keyword to differentiate between the local and sophistication variables.
As an exercise, predict the output of following Java program.
public class Test { static int x = 11; private int y = 33; public void method1(int x) { Test t = new Test(); this.x = 22; y = 44; System.out.println("Test.x: " + Test.x); System.out.println("t.x: " + t.x); System.out.println("t.y: " + t.y); System.out.println("y: " + y); } public static void main(String args[]) { Test t = new Test(); t.method1(5); } }
Output:
Test.x: 22 t.x: 22 t.y: 33 y: 44
Loop Variables (Block Scope)
public class Test { public static void main(String args[]) { { // The variable x has scope within // brackets int x = 10; System.out.println(x); } // Uncommenting below line would produce // error since variable x is out of scope. // System.out.println(x); } }
Output:
10
As another example, consider following program with a for loop.
class Test { public static void main(String args[]) { for (int x = 0; x < 4; x++) { System.out.println(x); } // Will produce error System.out.println(x); } }
Output:
11: error: cannot find symbol System.out.println(x);
The right way of doing above is,
// Above program after correcting the error class Test { public static void main(String args[]) { int x; for (x = 0; x < 4; x++) { System.out.println(x); } System.out.println(x); } }
Output:
0 1 2 3 4
Let’s look at tricky example of loop scope. Predict the output of following program. You may be surprised if you are regular C/C++ programmer.
class Test { public static void main(String args[]) { int a = 5; for (int a = 0; a < 5; a++) { System.out.println(a); } } }
Output :
6: error: variable a is already defined in method go(int) for (int a = 0; a < 5; a++) ^ 1 error
Note:- In C++, it'll run. But in java it's a mistake because in java, the name of the variable of inner and outer loop must vary .
A similar program in C++ works. See this.
As an exercise, predict the output of the subsequent Java program.
class Test { public static void main(String args[]) { { int x = 5; { int x = 10; System.out.println(x); } } } }
Q. From the above knowledge, tell whether the below code will run or not.
Output :
class Test { public static void main(String args[]) { for (int i = 1; i <= 10; i++) { System.out.println(i); } int i = 20; System.out.println(i); } }
1 2 3 4 5 6 7 8 9 10 20
Yes, it'll run!
See the program carefully, inner loop will terminate before the outer loop variable is said .So the inner loop variable is destroyed first then the new variable of same name has been created.
Some Important Points about Variable scope in Java:
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.