1

The code that I want to use is

private void removeInstance (BaseClass b) {
    // some more code
    b = null;
}

b can be of an instance of a subclass of BaseClass. For example

public class C extends BaseClass {/* Some Code */}

This is what should be possible.

C c = new C ();
// some code
removeInstance (c);

However it currently does not set c to null as c is cast to BaseClass in this example.

I want to be able to set an instance to null from the base class in one way or another.

Emz
  • 1,280
  • 1
  • 14
  • 29
  • It's entirely unclear what you want to do. What should `removeInstance` do? You know you're not touching the actual object outside of `removeInstance`, right? Pass by value, pass by reference by value kind of stuff. – Jeroen Vannevel Feb 18 '15 at 13:46

1 Answers1

3

You can't do that. Java is a pass by value language. Setting the b variable to null doesn't affect the c variable passed to that method.

Eran
  • 387,369
  • 54
  • 702
  • 768