Lets say I have Class A, that calls static methods in either Class B or Class C. From which class it will call will depend on a parameter passed in the constructor of Class A. The methods in Class B and C that need to be called have identical method signatures. So, is there any way to do something like:
Object call;
if (type == 0)
call = ClassA;
else
call = ClassB;
call.someMethod ();
I know it can be done using an interface and having both class B and C implement the interface but that would require creating an object of B or C each time the call above happens which is quite resource intensive. So, I would like to know if there are any other ways to do this.
Edit: Sorry if this was not clear, but the Object call is a class variable and method calls to Class B and C need to be made in other methods in the class not just the constructor.
Don’t use static methods. Use an interface (or a base class), create a single instance of both of your classes and store them in static fields. That way you don’t have to instantiate every time, just take an existing instance from the fields.