The value will be 5. Although all parameters are passed by value, in the case of <? extends Object>
the value of ?
is an address that references the Object
, so whats is happening when you pass an Object
is that its address is passes by value. Which means if you use that Object
its address is the same as the one that got passed, so while you cant assign a new
Object
to it you can pretty much do what ever you want with its attributes and this will change the original Object
's state.
consider this class example:
public class Test {
public int i;
public Test(int i) {
this.i = i;
}
}
and this static method:
public static void changeAtrib(Test t) {
t.i = 0;
t = new Test(7);
}
now lest create an instance of Test
and pass it to our method:
Test test = new Test(5);
changeAtrib(test);
When the method runs the variable t
inside holds the address value of the original test
.
The first line in the method is: t.i = 0;
Which is referencing i
using test
's original address, that means that i
will change in the original test
as well.
Now lets take a look at the second line: t = new Test(7);
Now remember t
holds the address value of test
and t = new Test(7);
only creates a new Test
object and assigns its address to t
, that operation has no impact on test
since it only changes the value t
hold and makes t
reference a new object.