Download presentation
Presentation is loading. Please wait.
Published byRidwan Teguh Sasmita Modified over 5 years ago
1
Local variables and how to recognize them
2
Purpose of local variables
A local variable will exists as long as the method in which they have been been created is still running As soon as the method terminates (i.e., returns), all the local variables defined inside the method are destroyed. A local variable is used to store information that is relevant for the duration of the execution of one method
3
Defining (recognizing) local variables
How to the define a local variable: A local variable is defined inside the body of a method (I.e., a local variable is defined between the opening and closing braces of a method)
4
Defining (recognizing) local variables (cont.)
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, ); r = MyProgram.min( -9.9, 3.8 ); }
5
Defining (recognizing) local variables (cont.)
public class ToolBox { public static double min ( double a, double b ) { // Body of method "min" double m = 0; // *** Local variable !!! if ( a < b ) m = a; // a is the smaller value } else m = b; // b is the smaller value return(m);
6
Defining (recognizing) local variables (cont.)
Next, we study the life time and the scoping rules for local variables
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.