• 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

Reference Problem

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is one class Car and another one called Tyre. Now , with the destruction of the Car object we want its associated tyre(s) to be destructed with it. Consider the following code

class Car {
Tyre t ;

Car( Tyre t1 ) { t = t1; }
}

class Tyre {
}

class Test {

Tyre t = new Tyre();
Car a = new Car(t);
...
...
...
...

a = null ; // I have destroyed the Car Object , but i can still refer the tyre object through t reference.

}


Problem : - I want to do something so that i can enforce this thing that whenever i make car reference equal to null then there is no way to access tyre(s) associated with it.
How would i achieve this or enforce this ???

 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sahil,

basically you have to take care that there are no unused references to Tyre instances in order to make all tyres eligible for garbage collections as soons as a car they belong to dies. In your example the easiest way would be to null out the variable "t" in your Test class after the Car is created or to instantiate the Tyre directly inside the paranthesis of the Car constructor like

Additionally there are more advanced features like SoftReference or WeakReference which could be use to avoid holding strong references to an object which prevents garbage collection.

Marco
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The longevity of the t reference to the Tyre depends on where its declared. For example if its delcared locally within a method of the Test class then it will be eligible for garbage collection once the method ends, since there is no way to access the reference then.

Also Sahil, after 316 posts surely you know that
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you create a new tyre within your Test method, it will not be eligible for the garbage collector until after the Test method has completed. You more likely would like to design this so you assign the tyre to the car within the car's constructor. Depending upon what this actual program will be you may want to think about using the Builder Pattern as well.
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic