Short answer: No.
Longer answer:
Instances cannot be null
at all. Variables that are typed as references to the class can be set to null
, but not instances. There can be many variables referring to the same instance. The instance has no way of knowing about those variables, so there's no way for it to reach out and set them to null
.
Instead, you could have a method that returns null
with the return type of the class:
public ClassA clear() {
return null;
}
...which the caller could use
foo = foo.clear();
But it doesn't really serve any purpose, the caller could just as easily do
foo = null;
Side note:
In .Net (since you mention C#), you might look at the IDisposable
interface.
In Java, you might look at AutoCloseable
.