12 Replies Latest reply: Sep 18, 2007 2:30 PM by 807605 RSS

    Java static keyword

    807605
      In the code listed below, it seems the static section is only run once per the very first call to using the class, which is in this case 'SomeClass'. Is there any way to get the static section to run again? Is the only way by restarting the java runtime instance? Thanks.
      public class SomeClass {
        private static int someNum=-1;
        static {
          someNum++;
        }
        private static int getSomeNum() {
          return someNum;
        }
      }
      Edited by: Greg439 on Sep 18, 2007 7:25 PM
        • 1. Re: Java static keyword
          796447
          What are you really wanting to have done?
          A static initializer block initializes the class, ONCE (or never, if you never use the class at all).
          If you're wanting to increment a counter every time you call a method, then do that in the method's implementation.
          • 2. Re: Java static keyword
            DrClap
            Restarting the JVM is basically the only way to get it to run again.

            But if you need that code to run more than once, you should not have put it in a static initializer in the first place. Moving it to the correct place would be a better approach than looking for hacky workarounds.
            • 3. Re: Java static keyword
              807605
              Yes, it's only run when the class is loaded. Even if you got the class to load again, it would be a fresh copy and someNum would be incrementing from -1 to 0. You never wrote what your problem is, what you are trying to do, what your goal is? Would a simple static method suffice?
              public static void increment() {
                  someNum++;
              }
              • 4. Re: Java static keyword
                807605
                I was trying to understand the lifecycle aspect of using a static class. The incrementation of someNum was only a nonpractical example.

                Thanks for all of your help!
                • 5. Re: Java static keyword
                  807605
                  static class
                  Are you sure you even know what a static class is? Like this?
                  public class Outer {
                      public static class ImStatic {}
                  }
                  • 6. Re: Java static keyword
                    796447
                    {color:#ffffff}Reply deleted{color}
                    • 7. Re: Java static keyword
                      807605
                      warnerja wrote:
                      {color:#ffffff}Reply deleted{color}
                      To read this, print it out, squirt lemon juice on the paper, then hold over a flame until singed.
                      • 8. Re: Java static keyword
                        796447
                        Testing to see if there is a forum bug in leftover color tags:
                        {color:#800000}
                        This is colored red (if I got it right). Without another color tag to end it will the next reply also be in red?
                        • 9. Re: Java static keyword
                          796447
                          Darn. Without the proper {color} closing tag in the same post it has no effect.
                          • 10. Re: Java static keyword
                            DarrylBurke
                            {color:#000080}Hey Warner, you can use named colors also, but you do have to close the tags. Something like this --{color}

                            {color:red}{color:red}{color}

                            {color:darkgreen}{color:darkgreen}{color}

                            {color:blue}{color:blue}{color}

                            {color:#000080}CSS style attributes, actually. But I couldn't find any other attributes that work.

                            db{color}
                            • 11. Re: Java static keyword
                              807605
                              Let me see:

                              {color:CornflowerBlue}CornflowerBlue{color}

                              {color:PapayaWhip}PapayaWhip{color}

                              woo-hoo

                              I dibs {color:SlateBlue}SlateBlue{color}
                              • 12. Re: Java static keyword
                                796447
                                Darryl.Burke wrote:
                                {color:#000080}Hey Warner, you can use named colors also, but you do have to close the tags. Something like this --{color}

                                {color:red}{color:red}{color}

                                {color:darkgreen}{color:darkgreen}{color}

                                {color:blue}{color:blue}{color}

                                {color:#000080}CSS style attributes, actually. But I couldn't find any other attributes that work.

                                db{color}
                                {color:darkgreen}Thanks db{color}