|
|
(I.e., a local variable is defined between the opening and closing braces of a method)
Example:
public class MyProgram { public static void main(String[] args) { // Body of method "main" double r; // *** Local variable !!! r = MyProgran.min( 1.0, 4.0 ); System.out.println(r); r = MyProgram.min( 3.7, -2.9 ); System.out.println(r); r = MyProgram.min( -9.9, 3.8 ); System.out.println(r); } } |
Next, we study the life time and the scoping rules for local variables
|
|
|
|
|
|
![]() |
![]() |
The current program location is given by a green arrow:
![]() |
Note:
|
|
![]() |
public class FindFactors { public static void main(String[] args) { // <----- start block 1 Scanner in = new Scanner(System.in); int n; int f; System.out.print("Enter integer: "); n = in.nextInt(); f = 2; // start while ( n != 1 ) { // <----- start block 2 if ( n % f == 0 ) { // <----- start block 3 System.out.println(f); n = n / f; } // <----- end block 3 else { // <----- start block 4 f++; // Try next number } // <----- end block 4 } // <----- end block 2 } // <----- end block 1 } |
![]() |
I did this to delay the discussion on scope rules for local variables.
When you do this, there are consequences about where you can access the variable
(A local variable defined at the start of a method is accessing everywhere inside the method - that's why I don't need to talk about scoping rules until now....)
The scope of a variable is the region of the program in which the variable can be accessed |
The scope
of a
local variable
extends
|
public class FindFactors { public static void main(String[] args) { // <----- start block 1 Scanner in = new Scanner(System.in); int n; int f; System.out.print("Enter integer: "); n = in.nextInt(); f = 2; while ( n != 1 ) { // <----- start block 2 if ( n % f == 0 ) { // <----- start block 3 System.out.println(f); n = n / f; } // <----- end block 3 else { // <----- start block 4 f++; // Try next number } // <----- end block 4 } // <----- end block 2 } // <----- end block 1 } |
![]() |
You can see that the scope of these variables is the "yellow" rectangle".
The other rectangles are contained inside the "yellow" rectangle" - these variable are accessible inside its rectangle (the "yellow" rectangle") and all rectangles that is contained in the "yellow" rectangle".
public class RectangleTester { public static double area(Rectangle rect) { // <---- Start block 1 double r = rect.getWidth() * rect.getHeight(); // (1) return r; } // <---- End block 1 public static void main(String[] args) { // <---- Start block 2 Rectangle r = new Rectangle(5, 10, 20, 30); //(2) double a = area(r); System.out.println(r); }// <---- End block 2 } |
So when a variable named r is mentioned inside this range, Java will identify the name r with the variable double r (because that's where this variable is accessible)
So when a variable named r is mentioned inside this range, Java will identify the name r with the variable Rectangle r (because that's where this variable is accessible)
public class Scoping1 { public static void main(String[] args) { int i = ...; if ( i > 15 ) { int j = 20; // (A) System.out.println(j); // (1) } else { String j = "Hello"; // (B) System.out.println(j); // (2) } System.out.println(j); // (3) } } |
This is an ERROR !!!
When you compile this program you will see the error:
Scoping1.java:20: cannot find symbol
symbol : variable j
location: class Scoping1
System.out.println(j); <<--- occured in line 20
In other words: the parameter variables of a method is accessible anywhere within the body of the method...