2

I have a situation where a subclass (let's call it SubClass), during class initialization, calls a static method of the base class (let's call it BaseClass) and passes SubClass.class as a parameter to identify the specific class being initialized, or in Java:

public class SubClass extends BaseClass {
    static { BaseClass.init(SubClass.class); }
    ...
}

Instead of BaseClass having the method:

protected static void init(Class<?> c) {
    ...
}

what I would like to do is have BaseClass call init automatically, such as:

public class BaseClass {
    static { init(thisclass); }
    ...
}

So, the question is, does Java have some keyword, represented by thisclass above, that returns the class being initialized in a class initializer?

John Jackson
  • 227
  • 1
  • 9
  • 1
    If you had multiple subclasses, how would the one base class static initializer initialize multiple of them? – Andy Thomas Aug 13 '16 at 00:17
  • The class being initialized in `BaseClass`'s static initializer blocks is always `BaseClass`. If you're expecting that initializer to run for subclasses, you'll need to adjust your expectations. – user2357112 Aug 13 '16 at 00:19
  • Perhaps think of this as an abstract problem. I have boiled down the problem from my implementation, which works just fine, but I want to avoid having to tell the base class explicitly which subclass is being initialized. In practice, I am storing the value SubClass.class as a key to a Map; the value to the map is information that BaseClass is tracking for each subclass. – John Jackson Aug 13 '16 at 01:56
  • Various instance methods in BaseClass recall this information by accessing the Map using the key this.getClass(). – John Jackson Aug 13 '16 at 02:01

2 Answers2

3

No, there isn't.

Also, as user2357112 said in the comments, the static initializer for BaseClass is only going to run once, while initializing BaseClass. It isn't like an instance constructor, run every time an instance of a subclass is created. Basically you should stick to your existing scheme.

It's also worth noting that if you're expecting subclasses to sort of "register themselves" with something, you may be surprised at when subclass initialization doesn't occur.

For example, if you have a static method foo in the base class, then calling Subclass.foo() won't initialize Subclass - only the base class, because the subclass doesn't need to be initialized, as none of its code is executing.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • OK, thanks! I missed the part about the base-class initializer running only once; I should have known that already. While the current implementation works, I'll have to rethink the problem I am trying to solve to see if there is a better way. – John Jackson Aug 13 '16 at 02:08
  • Thanks for all of your comments! I reworked the implementation so that the base class calls its `init` method itself on behalf of a subclass the first time an instance method of the base class notices that the subclass is not registered. This, unfortunately, costs a run-time check in the base class's instance methods, but it makes the registration fully automatic. – John Jackson Aug 13 '16 at 23:42
-2

No there isn't, because there is no instance at this point. Just as in a static method.

user207421
  • 305,947
  • 44
  • 307
  • 483