Hi! Folks
Local variables in methods go out of scope when the method exits. At this point the methods are eligible for garbage collection. Each time the method comes into scope the local variables are re-created.
i've doubts:
1. In the above statement what does methods are eligible for garbage collection mean?
2. Does variables in method r candidates/eligible for gc??? yes, i know..once method exits variables go out of scope...but variables r not garbage collected.
i'm confused...
class A
{
String s;
A(String s) { this.s =s;}
public void finalize() { System.out.println(s);}
}
public class BDemo
{
double d;
public void static m1()
{
A a1 = new A("hello"); // 1
String s = new String("destroy"); //2
int j= 10; //3
String s = "s1"; //4
String s2 = "s2"; //5
String s2=s;
}
public static void main(String args[])
{
BDemo.m1(); System.gc();
}
}
In the above program when the method exits the variables 'n'objects created in the method go out of scope. Therefore "hello" 'n' "destroy" are eligible for gc as they r objects. What about j,s,s2 ??? R they candidates for gc.
Thank you in advance.
jaya
Local variables in methods go out of scope when the method exits. At this point the methods are eligible for garbage collection. Each time the method comes into scope the local variables are re-created.
i've doubts:
1. In the above statement what does methods are eligible for garbage collection mean?
2. Does variables in method r candidates/eligible for gc??? yes, i know..once method exits variables go out of scope...but variables r not garbage collected.
i'm confused...
class A
{
String s;
A(String s) { this.s =s;}
public void finalize() { System.out.println(s);}
}
public class BDemo
{
double d;
public void static m1()
{
A a1 = new A("hello"); // 1
String s = new String("destroy"); //2
int j= 10; //3
String s = "s1"; //4
String s2 = "s2"; //5
String s2=s;
}
public static void main(String args[])
{
BDemo.m1(); System.gc();
}
}
In the above program when the method exits the variables 'n'objects created in the method go out of scope. Therefore "hello" 'n' "destroy" are eligible for gc as they r objects. What about j,s,s2 ??? R they candidates for gc.
Thank you in advance.
jaya