0

I have a class, named GlowZombie, which has two contructors:

public class GlowZombie extends GlowMonster implements Zombie {

    public GlowZombie(Location loc) {
        this(loc, EntityType.ZOMBIE);
    }

    public GlowZombie(Location loc, EntityType type) {
        super(loc, type, 20);
    }

And that class has an inner-class, GlowHusk, with the same constructor parameters:

    public class GlowHusk extends GlowZombie implements Zombie.Husk {

        public GlowHusk(Location loc) {
            this(loc, EntityType.HUSK);
        }

        public GlowHusk(Location loc, EntityType type) {
            super(loc, type);
        }
    }
}

Then, I need to use reflection to get the GlowHusk(Location) constructor, like such:

Constructor<T> constructor = (Constructor<T>) GlowZombie.GlowHusk.class.getConstructor(Location.class);

...Which throws exception:

java.lang.NoSuchMethodException: net.glowstone.entity.monster.GlowZombie$GlowHusk.<init>(org.bukkit.Location)

Note:

Using GlowZombie instead of GlowHusk executes correctly:

Constructor<T> constructor = (Constructor<T>) GlowZombie.class.getConstructor(Location.class);

And does not throw an exception.

The question: why does getConstructor() not work in this scenario? Would it be because it is an inner-class, thus requiring some other way of accessing its constructor?

Momo
  • 3,542
  • 4
  • 21
  • 34
  • Is `GlowHusk` an inner class of `GlowZombie`? – Sotirios Delimanolis May 21 '16 at 22:09
  • @SotiriosDelimanolis Yes, it is an inner-class of GlowZombie. I added the question at the bottom for clarification. – Momo May 21 '16 at 22:13
  • Since the *nested* class `GlowHusk` extends the *outer* class `GlowZombie`, are you sure you meant for `GlowHusk` to be an *inner* class? I suspect that you meant for it to be a *static nested* class, in which case you need to add the `static` keyword to class `GlowHusk`, and then your `getConstructor()` call will work as-is. If you don't know the difference, see [The Java™ Tutorials - Nested Classes](https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html). – Andreas May 21 '16 at 22:16
  • The problem is that `GlowHusk` class belongs to the instance of `GlowZombie`, not to the class `GlowZombie` - i.e. it is not static. – Jaroslaw Pawlak May 21 '16 at 22:17

0 Answers0