• Post Reply Bookmark Topic Watch Topic
  • New Topic

garbage collection  RSS feed

 
mrudul joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is code:
class B {
private String name;
public B(String s) {name = s;}
public void finalize() {System.out.print(name);}
}
class E {
public static void m() {
B x1 = new B("X"), y1 = new B("Y");
}
public static void main(String[] args) {
m(); System.gc();
}}
In this code,following steps will take place.
1 m()will call the public static method m()
2 it will create the object of class B and it will then go to
constructor for class B.
3 there it will store "X" under the name of <name>
4 now second object will be created and it will store "Y" under the variable <name>
will it make two copies of name, each storing different string or will the second string overwrite the first one?
thank you.
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mrudul joshi:


Mrudul, next time use the code tag around your code to make them easier
to read.
Regarding your question(I think), the name is not overwritten since you have
2 different objects which are x1 and y1. And each of them has their own name.
Just like you and I are both in class Human, and we both have our own name.
So both of your assumption/understanding were wrong, for they do not:
1. overwrite the name
2. keep 2 names, since there is only 1 name to each object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!