简体   繁体   中英

Is it possible to acces this.getClass() before calling super()

I want to call this.getClass() before calling the constructor with super(...) .

abstract class A extends SuperClass {
    public A() {
        super(Manager.someOtherMethod(this.getClass()))
        // Does not work: "Cannot reference 'Object.getClass()' before
        // supertype constructor has been called".
    }
}

I read about a workaround using static methods, but this.getClass() cannot be called from a static context.

This question depends on my previous question .

Thanks in advance for you answers :)

Whilst I am dubious about the need for this, you can do it by inserting the call to Manager.someOtherMethod into the constructor of the superclass:

class SuperClass {
  SuperClass() {
    Object result = Manager.someOtherMethod(this.getClass());
  }
}

class A extends SuperClass {}

class Manager {
  static Object someOtherMethod(Class<?> clazz) {
    System.out.println(clazz);
    return new Object();
  }
}

would print out

class A

(if you create an instance of A )

Ideone demo


If SuperClass has multiple constructors, you can assign the result to a field in the superclass:

class SuperClass {
  private final Object result = Manager.someOtherMethod(this.getClass());

  SuperClass() { ... }

  SuperClass(String someParameter) { ... }
}

then result will be available in both constructors.

I suggest you to refactor your code.

I assume that you have classes A1 , A2 and A3 now.

You should introduce factory like this:

class AFactory {
    public static A1 newA1() {return new A1(A1.class);}
    public static A2 newA2() {return new A2(A2.class);}
    public static A3 newA3() {return new A3(A3.class);}
}

Add parameter to classes A1 , A2 and A3 constructors.

Add parameter to A constructor.

...what's the question? Do I understood correctly, do you want the class to be subclassed, and THAT class should be given to the super, but you don't want to call super in all the subclasses?

Indeed it can't be done this way as the class object is not constructed yet, and therefore it does not exist. So you cannot call a method on the object yet, or ask for the name, or whatsoever.

I think you're stuck with either a class passing on to super, or add a pretty useless public static class<? extends SuperClass> getThisClass() { ... } public static class<? extends SuperClass> getThisClass() { ... } or am I missing something nice? if I do, pls comment, as I actually like to know myself too.

Just for curiousity I tried different ways which are stated here: Getting the class name from a static method in Java but the closest I got was getting that class test.staticgetclass.A in the middle class, but never its subclass name which I think you want to get.

Use a class literal as a constructor parameter

abstract class A extends SuperClass {
    protected A(Class<? extends A> clazz) {
        super(Manager.someOtherMethod(clazz));
    }

Then in impl:

class B extends A {
    public B() {
        super(B.class);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM