Re: Dynamic class loading questions
"Kris Ardis" <Kristopher.Ardis-6tN4nzCoH/[email protected]>
| Newsgroups | gmane.comp.hardware.microcontrollers.tini |
|---|---|
| Message-ID | <008d01c3f7d1$1ed61d00$764000b4@kardis> |
Chuck
What is happening on the current release is that static initializers in
dynamic classes aren't getting run until there is an access to a static
field or a static method in that class. In future revisions, dynamic
classes will always run their static initializers on class load. For now,
the workaround is to touch something static in your code...
public class dynloaded{
public int instancevar = 0;
public static int staticvar = 0;
public dynloaded(){
System.out.println("Default constructor for class dynloaded");
System.out.println("Static variable is: "+staticvar); /*
Workaround: CLINIT will run here */
}
static{
System.out.println("hello from static initializer inside
dynloaded");
staticvar++;
}
}
Kris
----- Original Message -----
From: "Chuck Wright" <[email protected]>
To: <TINI-6tN4nzCoH/[email protected]>
Sent: Thursday, February 19, 2004 2:51 PM
Subject: [TINI]Dynamic class loading questions
> Hi,
>
> I posted this question in January, and haven't seen a
> response, and none in the archive, so here's another
> try...This is definitely a question for the dalsemi
> guys.
>
> Thanks,
>
> - C
>
> -------------------------------------------------------
>
> Hello all,
>
> Apologies if this is old ground, but I do not find an
> answer in the archives or the distribution.
>
> Based on a small experiment, it appears to me that
> a static initializer in a dynamically loaded class does
> not get executed. (I am using 1.12, on a 400)
>
> In Limitations.txt I see a discussion of static initializers
> being handled slightly differently, but no mention of
> them with regard to dynamic class loading.
>
> Is this the expected behavior?
>
> Thanks!
>
> - C
>
> public class dyn{
>
> public static void main(String args[])
> throws ClassNotFoundException{
> System.out.println("Loading dynoaded");
> Class c = Class.forName("dynloaded");
> System.out.println("Loaded dynloaded, creating instance");
>
> try{
> c.newInstance();
> System.out.println("done");
> }catch(Exception e){
> System.out.println("error instantiating" + e);
> }
> }
>
> static{
> System.out.println("Hello from static initializer for dyn");
> }
> }
>
> public class dynloaded{
>
> public int instancevar = 0;
>
> public dynloaded(){
> System.out.println("Default constructor for class dynloaded");
> }
>
> static{
> System.out.println("hello from static initializer inside dynloaded");
> }
> }
>
> Output running dyn on PC:
>
> Hello from static initializer for dyn
> Loading dynoaded
> hello from static initializer inside dynloaded
> Loaded dynloaded, creating instance
> Default constructor for class dynloaded
> done
>
> On TINI:
>
> Hello from static initializer for dyn
> Loading dynoaded
> Loaded dynloaded, creating instance
> Default constructor for class dynloaded
> done
>
> --
> Chuck Wright
> http://chuck-wright.com
>
>
> _______________________________________________
> TINI mailing list
> TINI-6tN4nzCoH/[email protected]
> To UNSUBSCRIBE, edit your profile, or see list archives:
> http://lists.dalsemi.com/mailman/listinfo/tini
>
_______________________________________________
TINI mailing list
TINI-6tN4nzCoH/[email protected]
To UNSUBSCRIBE, edit your profile, or see list archives:
http://lists.dalsemi.com/mailman/listinfo/tini