0

Why there are two “a”s

public class Program { static int method(int a){ return ++a; } public static void main(String[] args) { int b=3; int a=4; System.out.println(method(a)); } }

22nd Apr 2020, 6:42 AM
Zhengrong Yan
Zhengrong Yan - avatar
4 Respostas
+ 6
Well, now it is clearer 👍 Yes, they are different. static int method(int a) * Here <a> is a function parameter. public static void main(String[] args) int a = 4; * Here <a> is a local variable. Now I understand why you got confused, I also make similar mistake at times. Using same name for parameter and argument can be confusing especially when our mind isn't so clear. Use of meaningful variable names helps for this types of problem.
22nd Apr 2020, 7:27 AM
Ipang
+ 2
Can you be more specific here? there are loads of letter 'a' in that code. I'm not getting what you mean here.
22nd Apr 2020, 7:01 AM
Ipang
+ 2
The a in the main method is outside the scope of the a in the other method. They can't see each other. Java is a pass by value, so when the a from main is passed to method during the method call, a copy of its value is passed to the a inside method.
22nd Apr 2020, 7:28 AM
ChaoticDawg
ChaoticDawg - avatar
0
i don't understand why there are two “a”s are defined with int . Are they different?
22nd Apr 2020, 7:13 AM
Zhengrong Yan
Zhengrong Yan - avatar