GNOME Bugzilla – Bug 577149
Unable to use static field with an object in a different class
Last modified: 2010-03-26 17:04:11 UTC
A static field with an object from some class is null when used in an other class. Here's an example: public class Bar { public static /*const*/ Bar STATICBAR = new Bar (); public string printme { get; set; default = "bar"; } } public class Foo { public void func ( Bar bar ) { stdout.printf ( "%s", bar.printme ); } public static int main ( string[] args ) { var c = new Foo (); c.func ( Bar.STATICBAR ); return 0; } } The string is never printed as bar is null and the parameter assertion of the method func fails. The code does work when the string printme and the static field are moved to the Foo class (so the static field then holds a Foo object). I've commented const for STATICBAR as this causes the C code to be invalid (duplicate variables) but this has already been reported.
I think non-constant initializers are not valid in this context (see also https://bugzilla.gnome.org/show_bug.cgi?id=569259), but Vala fails to recognize this. Instead, it just does not create the actual object.
Thanks for the bug report. This particular bug has already been reported into our bug tracking system, but please feel free to report any further bugs you find. *** This bug has been marked as a duplicate of bug 543189 ***