let suppose there are two variables both have the same name one is declared globally and another is declared locally…so is it possible that we declare two variables with same name at different scopes…if yes then how value is printed global variable or local variables
Function demo global variables
public class Main {
static int n = 10;
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int n = 5;
System.out.println(n);
test();
}
public static void test() {
System.out.println(n);
}
}
Here the output will be:
5
10
In the method where local variable is defined the local value will be used. In the method where the local variable is not defined, global will be used.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.