package Example1;
public class LocalVsGlobal {
static int interest=10;//global variable
public static void main(String[] args) {
// TODO Auto-generated method stub
display();
System.out.println("inside the main method "+interest);
}
public static void display(){
int interest=8;// local variable
System.out.println("Inside the display method "+interest);
}
}
output:Inside the display method 8
inside the main method 10
The value of interest inside display method uses the local variable value which is 8, while the interest value inside main method uses gloabl variable and that is the reason it is different.
If we want the gloabl variable value to be updated with the local variable value then just remove the keyword int it and it affects all value in all places. >>>Pass By value
------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment