Question about how static member/method inheritance works in apex:
For reference, this question is related, but doesn't address my question or take into account my findings: reference question
I have a factory base class, with only static members and methods that are not allowed to be declared virtual:
public virtual class Fact_BaseFactory
{
/******** MEMBERS REGION ********/
private static Map<String, Schema.DisplayType> fieldNameToTypeMap = new Map<String, Schema.DisplayType>();
private static Map<String, ID> recordTypes = new Map<String, ID>();
/******** PRIVATE HELPER REGION ********/
//attaches newly inserted, factory-produced lookup record
private static void populateFieldNameToTypeMap(String objectName)
{
List<String> objNames = new List<String>();
objNames.add(objectName);
Schema.DescribeSobjectResult objDescribe = Schema.describeSObjects(objNames)[0];
for (Schema.SObjectField objectFields : objDescribe.fields.getMap().values())
{
Schema.DescribeFieldResult fieldDescribe = objectFields.getDescribe();
fieldNameToTypeMap.put(fieldDescribe.getName(), fieldDescribe.getType());
}
}
}
Which I use to extend an SObject factory from:
global class Fact_Account extends Fact_BaseFactory
{
public class AccountFactoryException extends Exception { }
//define default constants
public static final String DEFAULT_NAME = 'FactoryAccount';
public static final String DEFAULT_ENTITY_Id = 'Foo_North_America';
/******** STATIC INITIALIZATION REGION ********/
static
{
//base class method
super.populateFieldNameToTypeMap('Account');
//base class member
recordTypes = UtilityMethods_Data.mapOfAccountRecordTypes();
}
//rest of class
}
My question is about the SUPER keyword seen here in the static initialization block of the subclass. The code as shown compiles correctly, but if I remove the super keyword, I get a 'Variable does not exist: recordTypes' error because the static members/methods are not inherited from the base class.
However, because I use the super keyword to reference a method from the base-class, somehow this allows me to directly reference all of the static members/methods of the base class in the subclass (without super keyword), as long as I use the super keyword to call a base-class method (HAS to be method) before the super-less base-class reference (member OR method).
Without the super keyword, the compiler seems to search for base-class members first to error on, and if there are no base-class members it errors on base-class method references. I think that's due to compilation-step order and not because of different treatment of static member vs static method on the base class.
- Is this a known/acceptable use of SUPER to enable inheritance from one wholly static class to another wholly static class?
- Is this a poor design because it's not readable/maintainable anyway, with two options I can see to replace:
a) Change Fact_BaseFactory into a utility class (Fact_FactoryHelper) with all static methods, and move static members from base into ex-subclasses (violates DRY).
b) Composition instead of inheritance.
Thanks!
public class MyClass<T> { }
, but then not allowing you to re-compile the code.