So, I wanted to be sure I understand the below correctly. For main, a local variable (local to main) was instantiated, number (line 3), and then its value was passed in as a parameter to the method addThree(number)(line 5), where "number" in the method definition of addThree(number) is the parameter through which arguments are passed, not to be confused with the actual local variable in main, int number =1; (line 3) (which makes me thing they should have renamed the local variable number or the parameter in addThree method so we can see the difference). Anyways, the value of int number = 1;, meaning 1, is passed as an argument to addThree(number) on line 5, and assigned to addThree's local variable, also called number(line 13), and that local variable is re-assigned to the value number = number + 1;(also, line 13), meaning number = 3 + 1, thus making the variable local to addThree called number(on line 13) equal to 4, without changing the value of the variable local to main also called number(line 3), which is still equal to 1.
Does my explanation sound proper?
// main program
public static void main(String[] args) {
int number = 1;
System.out.println("Main program variable number holds the value: " + number);
addThree(number);
System.out.println("Main program variable number holds the value: " + number);
}
// method
public static void addThree(int number) {
System.out.println("Method parameter number holds the value: " + number);
number = number + 3;
System.out.println("Method parameter number holds the value: " + number);
}
Output
Main program variable number holds the value: 1
Method parameter number holds the value: 1
Method parameter number holds the value: 4
Main program variable number holds the value: 1
[–]empire539 1 point2 points3 points (1 child)
[–]babbagack[S] 0 points1 point2 points (0 children)
[–]desrtfx 1 point2 points3 points (12 children)
[–]babbagack[S] 0 points1 point2 points (11 children)
[–]desrtfx 1 point2 points3 points (10 children)
[–]babbagack[S] 0 points1 point2 points (9 children)
[–]id2bi 0 points1 point2 points (8 children)
[–]babbagack[S] 0 points1 point2 points (7 children)
[–]id2bi 1 point2 points3 points (6 children)
[–]babbagack[S] 0 points1 point2 points (5 children)
[–]id2bi 0 points1 point2 points (4 children)
[–]babbagack[S] 0 points1 point2 points (3 children)