3

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 :)

Community
  • 1
  • 1

4 Answers4

2

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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • The thing is, that my superclass needs the return value of the `Manager.someMethod()` in its constructor. – Schokokuchen Bäcker Sep 02 '16 at 14:32
  • Well, the superclass is the one calling `Manager.someMethod(this.getClass())`; so just do something with the return value (assign to a field, assign to a local variable...). – Andy Turner Sep 02 '16 at 14:41
  • I do not need to return anything from a constructor. I need the return value of a static method, which needs a reference to the initialization class (`this.getClass()`), to be used for the constructor of the superclass. I cannot call `Manager.someMethod()` in the superclass, but it needs the methods return value. – Schokokuchen Bäcker Sep 02 '16 at 14:42
1

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.

talex
  • 17,973
  • 3
  • 29
  • 66
  • Since this solved the problem I marked it as the correct answer. Though i still would like to know wheter there is another, easier solution. – Schokokuchen Bäcker Sep 02 '16 at 14:39
  • It's really not clear why this would be necessary - there's definitely no reason why you'd need to pass a class object into the constructor of that class. – Oliver Charlesworth Sep 02 '16 at 15:00
  • @OliverCharlesworth It isn't necessary. You can just have consructors like `A1(){super(A1.class);}`. But my aproach will be simplier in case of big class hierarchy because in other case you will need to have two constructor. – talex Sep 05 '16 at 09:35
0

...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() { ... } 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.

Community
  • 1
  • 1
Jeroen van Dijk-Jun
  • 1,028
  • 10
  • 22
  • This question was created when i had my [previous question](http://stackoverflow.com/questions/39291631/refering-to-subclass-from-abstract-class) answered. I think it should help clarify my intention. – Schokokuchen Bäcker Sep 02 '16 at 14:30
0

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);
    }
Bohemian
  • 412,405
  • 93
  • 575
  • 722