teo kokos wrote:...My problem is in P2 method.
a) Why the local X and Y have the value 5?? :O
b) Why the static Y has value 350? With this logic in P1 method the static Y should have value 160...
I'm glad to see you're working on this! It's a great way to learn.
The method P2 has a local variable X because X is declared as a parameter: static void P2(
int X) {...}. But the method P2 does
not have local A or Y variables. So in the P2 method body, X refers to a local variable, but A and Y both refer to the static class variables. That means inside P2, A is the same as TestApp1.A, and Y is the same as TestApp1Y.
Also, note that the value passed to P2 is referred to as
X (the local variable). However, when P2 is called in main, it's called by passing the incremented value of the static variable
Y.
Before that method is called, Y is 4. So calling P2(++Y) increments Y to 5, and passes that value to P2, where it's assigned to the local variable X. So inside P2, X refers to the local variable X, which is 5, and Y refers to the static variable Y, which is also 5.