Wednesday, 16 November 2016

garbage collection - In java, how can we destruct an instance of a class from a method within the class

NOTE: What you suggest is highly unlikely to be useful.


What you can do is use delegation.


class A {
private AImpl impl = new AImpl();
public void close() {
if (impl != null)
impl.close();
impl = null;
}
}

As all references are indirect, you can ensure there is only one reference to the real object and clear it.




Proxies in some OSGi containers do this when a component is unloaded. As the container has little control over the lifecycle of the references, it would make it difficult to ever unload a library (the implementation).

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...