I’m trying to pass an instance of a class to another object, but i can’t get it to work.
I currently have this:
class MyClass
{
public void myFunc()
{
OtherClass bla = new OtherClass(this);
}
public void callback()
{
// code...
}
}
class OtherClass
{
public OtherClass(Class<?> obj)
{
// do some stuff...
// Call method from obj
Method method = obj.getMethod("callback", SomeObject.getClass());
method.invoke(obj, SomeObject);
}
}
But now it gives me the following error:
The constructor OtherClass(MyClass) is undefined
So it is expecting me to give the class name hardcode in the constructor. How can i make it a generic type, so that it accepts all classes?
Should be:
And:
EDIT
Reading my code, and your comment again, this can be done in even a simpler way, by passing only one parameter:
and: