Just like instance fields are actually set in constructors, static fields are actually set in the static{} block of the class. In bytecode, that static{} block is represented by the static '<clinit>' method (class init method). So to initialize static fields, just add a 'static void <clinit>()' method to your bytecode and perform whatever operations you need in that method.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I am trying to create a class that can do this:
public class Test {
static Integer i=new Integer("1");
}
I can create the field, but I cannot figure out how to set the value to the field.
Any clues?
Thank you, George Hart
Just like instance fields are actually set in constructors, static fields are actually set in the static{} block of the class. In bytecode, that static{} block is represented by the static '<clinit>' method (class init method). So to initialize static fields, just add a 'static void <clinit>()' method to your bytecode and perform whatever operations you need in that method.