forked from zouldapp/JavaWorkshopCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPassByValue.java
45 lines (35 loc) · 1.28 KB
/
PassByValue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class PassByValue {
int x = 5;
public PassByValue () {
}
void incrementInt(int x) {
// pass a COPY of x (the value 5) to the doStuff method
System.out.println("x has value of " + x);
x += 5;
System.out.println("x has value of " + x);
}
private class InnerClass {
int x = 5;
public int getValue() {
return x;
}
public void setValue(int value) {
this.x = value;
}
}
public static void main(String[] args) {
PassByValue object = new PassByValue();
int y = 5;
object.incrementInt(y);
System.out.println("y has value of " + y);
PassByValue.InnerClass innerClassOne = object.new InnerClass();
PassByValue.InnerClass innerClassTwo = innerClassOne;
System.out.println("innerClassOne: " + innerClassOne.getValue()
+ " / hashCode: " + innerClassOne.hashCode());
System.out.println("innerClassTwo: " + innerClassTwo.getValue()
+ " / hashCode: " + innerClassTwo.hashCode());
innerClassOne.setValue(42);
System.out.println("innerClassOne: " + innerClassOne.getValue());
System.out.println("innerClassTwo: " + innerClassTwo.getValue());
}
}