java - Force static part of class to run without instantiation -
i have class:
public class textviewattachedproperties { public static final string namespace = "http://schemas.android.com/apk/lib/com.zworks.mvvmandroid"; private static final handler uihandler = new handler(looper.getmainlooper()); private static final string data_context = "datacontext"; private static final string text = "text"; private static final listener<propertychangedeventargs<string>> textchanged = new listener<propertychangedeventargs<string>>() { @override public void onevent(final propertychangedeventargs<string> args) { final textview element = (textview) args.getsource(); if (element != null && args.getnewvalue() != null) uihandler.post(new runnable() { @override public void run() { element.settext(args.getnewvalue()); } }); } }; // 2 statements have side effects i'm counting on execute private static final attachedproperty<object> datacontext = attachedproperty .newproperty(data_context, namespace, textview.class, null); private static final attachedproperty<string> text = attachedproperty .newproperty(text, namespace, textview.class, "", textchanged); }
and problem is run if instantiate class.
how can force run no matter what?
static initialization blocks run when jvm first sees mention of class. is, when class gets loaded.
you don't need create instance, need mention/reference class in way. there number of ways can trick.
Comments
Post a Comment