COMMUNITY

Access static variable of class passed into a function

As the title says, I am trying to access the static properties of a class passed into a function. This is a rough example of what I have done:

class Test {
    static function main() {
        testFunction(ChildClass1);
    }
    
    static function testFunction(c:Class<Dynamic>) {
        trace(c.staticVariable);
    }
}

class ParentClass {}

class ChildClass1 extends ParentClass {
    public static var staticVariable = "child1";
}

class ChildClass2 extends ParentClass {
    public static var staticVariable = "child2";
}

The problem is, the compiler throws me a compilation error:

Test.hx:7: characters 14-30 : Class<Dynamic> has no field staticVariable

So turns out the problem is that the compiler doesn’t recognise the c parameter of testFunction as a type of ParentClass. I’ve tried Class<ParentClass> and ParentClass<Dynamic> but they didn’t work. I tried googling, without success. Does anyone know how to solve this?

You can’t exactly pass objects through a function to get their static variable.
You’ll have to initialize the object to access it’s static variable if you’re going to access it through inheritance, here’s a better practice OOP example:

using Test;

class Test {
    static var exm:ParentClass;
    static function main() {
        
        testFunction(new ParentClass());
        testFunction(new ChildClass1());
    }
    
    public static function testFunction(c:ParentClass) {
        trace(c.getStaticVariable());
    }
}
    

class ParentClass {
    public static var staticVariable:String = "parent";
    
    public function new() {}
    
    public function getStaticVariable():String
    {       
        return ParentClass.staticVariable;
    }
}

class ChildClass1 extends ParentClass {
    public static var staticVariable:String = "child1";
    
    override public function getStaticVariable():String
    {       
        return ChildClass1.staticVariable;
    }
}

class ChildClass2 extends ParentClass {
    public static var staticVariable:String = "child2";
    
    override public function getStaticVariable():String
    {       
        return ChildClass1.staticVariable;
    }
}

You could also use Haxe Reflections but most I wouldn’t recommend because It could cause problems when targeting different targets. it but here you go:

class Test {
    static function main() {
        testFunction(ChildClass1);
    }
    
    static function testFunction(c:Class<ParentClass>) {
        trace(Reflect.field(c, "staticVariable"));
    }
}

class ParentClass {}

class ChildClass1 extends ParentClass {
    public static var staticVariable = "child1";
}

class ChildClass2 extends ParentClass {
    public static var staticVariable = "child2";
}

Hope this helps! :slight_smile:

2 Likes

Replace Class<Dynamic> with {staticVariable:String}

2 Likes

Thanks for the answer! I’ll see if I can alter my code so that it takes in a class instance instead of the class itself. If it fails then I’ll take a look at reflections. Thanks for your help! I really appreciate it.