Menu

how to set a value in a static field?

Help
2001-07-23
2001-08-10
  • george hart

    george hart - 2001-07-23

    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

     
    • Abe White

      Abe White - 2001-08-10

      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.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.