About Question com.enthuware.ets.scjp.v6.2.431 :
Posted: Fri Mar 18, 2011 5:19 pm
Hi,
I was wondering what happens if the method argument is not a reference of an object created outside the method, but instead is created "on the spot". For e.g.
class SayHello{
void hello(SayHello s){
System.out.println("hello");
}
public static void main(String[] args){
SayHello sh=new SayHello();
sh.hello(new SayHello());
}
}
Here instead of passing a reference to the SayHello object in hello(), we're creating the argument in the method itself. Will such an object be garbage collected? Thanks.
I was wondering what happens if the method argument is not a reference of an object created outside the method, but instead is created "on the spot". For e.g.
class SayHello{
void hello(SayHello s){
System.out.println("hello");
}
public static void main(String[] args){
SayHello sh=new SayHello();
sh.hello(new SayHello());
}
}
Here instead of passing a reference to the SayHello object in hello(), we're creating the argument in the method itself. Will such an object be garbage collected? Thanks.