w00
w00

Reputation: 26782

Pass unknown object to method in Java

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?

Upvotes: 2

Views: 10184

Answers (6)

MByD
MByD

Reputation: 137382

Should be:

OtherClass bla = new OtherClass(this.getClass(), this);

And:

public OtherClass(Class<?> clazz, Object obj)
{
    // do some stuff...

    // Call method from obj
    Method method = clazz.getMethod("callback", SomeObject.getClass());
    method.invoke(obj, SomeObject);
}

EDIT

Reading my code, and your comment again, this can be done in even a simpler way, by passing only one parameter:

OtherClass bla = new OtherClass(this);

and:

public OtherClass(Object obj)
{
    Class<?> clazz = obj.getClass();
    // do some stuff...

    // Call method from obj
    Method method = clazz.getMethod("callback", SomeObject.getClass());
    method.invoke(obj, SomeObject);
}

Upvotes: 3

Dangling Piyush
Dangling Piyush

Reputation: 3676

Use

public OtherClass(T obj)

instead.

Upvotes: 0

Martin McCallion
Martin McCallion

Reputation: 753

Your OtherClass constructor takes an object of type Class, but you're passing this to it, which is an object of type MyClass. I think your constructor should be OtherClass(Object obj).

Upvotes: 0

Joeri Hendrickx
Joeri Hendrickx

Reputation: 17445

Your logic is seriously flawed. The signature of your constructor expects a class, but you're invoking a method on it. Indicating it should be an object.

Also I don't see why you would do this; if you need to call a callback, just make an interface for it.

interface CallbackHaving{
  void callback();
}

class MyClass implements CallbackHaving
{
    public void myFunc()
    {
        OtherClass bla = new OtherClass(this);
    }

    public void callback()
    {
        // code...
    }
}

class OtherClass
{
    public OtherClass(CallbackHaving obj)
    {
        // do some stuff...

        // Call method from obj
        obj.callback();
    }
}

Upvotes: 0

Qwerky
Qwerky

Reputation: 18445

You are passing an instance on a MyClass object, when the constructor expects an instance of a Class object. You could try;

OtherClass bla = new OtherClass(this.getClass());

Upvotes: 2

titogeo
titogeo

Reputation: 2184

Use

public OtherClass(Class<? extends Object> obj)

Upvotes: 0

Related Questions