• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

garbage collection

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic