Can I define multiple static blocks?











up vote
13
down vote

favorite
2












Can I define multiple static blocks?



If possible, why should I define muliple static blocks?










share|improve this question




















  • 6




    For your first question, what happens when you try?
    – beny23
    Apr 4 '12 at 12:45










  • sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
    – jamesTheProgrammer
    Apr 4 '12 at 12:48















up vote
13
down vote

favorite
2












Can I define multiple static blocks?



If possible, why should I define muliple static blocks?










share|improve this question




















  • 6




    For your first question, what happens when you try?
    – beny23
    Apr 4 '12 at 12:45










  • sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
    – jamesTheProgrammer
    Apr 4 '12 at 12:48













up vote
13
down vote

favorite
2









up vote
13
down vote

favorite
2






2





Can I define multiple static blocks?



If possible, why should I define muliple static blocks?










share|improve this question















Can I define multiple static blocks?



If possible, why should I define muliple static blocks?







java static block






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 4 '12 at 12:46









Matt Fenwick

31.5k13102168




31.5k13102168










asked Apr 4 '12 at 12:43









user1127214

1,20971928




1,20971928








  • 6




    For your first question, what happens when you try?
    – beny23
    Apr 4 '12 at 12:45










  • sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
    – jamesTheProgrammer
    Apr 4 '12 at 12:48














  • 6




    For your first question, what happens when you try?
    – beny23
    Apr 4 '12 at 12:45










  • sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
    – jamesTheProgrammer
    Apr 4 '12 at 12:48








6




6




For your first question, what happens when you try?
– beny23
Apr 4 '12 at 12:45




For your first question, what happens when you try?
– beny23
Apr 4 '12 at 12:45












sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
– jamesTheProgrammer
Apr 4 '12 at 12:48




sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
– jamesTheProgrammer
Apr 4 '12 at 12:48












4 Answers
4






active

oldest

votes

















up vote
22
down vote



accepted










yes, you can also make multiple initialisation blocks.



This allows you to place code with the thing initialised.



private static final Map<String, String> map;
static {
// complex code to initialise map
}

private static final DbConnection conn;
static {
// handle any exceptions and initialise conn
}





share|improve this answer




























    up vote
    5
    down vote













    public class TryInitialisation {
    static int values = new int[10];
    static{
    System.out.println("running initialisation block");
    for (int i=0; i< values.length; i++)
    values[i] = (int) (100.0 * i);
    }
    static{
    System.out.println("running initialisation block");
    for (int i=0; i< values.length; i++)
    values[i] = (int) (200.0 * i);
    }
    static{
    System.out.println("running initialisation block");
    for (int i=0; i< values.length; i++)
    values[i] = (int) (300.0 * i);
    }
    void listValues(){
    for (int i=0; i<values.length; i++)
    System.out.println(" " + values[i]);
    }
    public static void main(String args) {

    TryInitialisation example = new TryInitialisation();
    example.listValues();
    example = new TryInitialisation(); // referencing a new object of same type
    example.listValues();
    }

    }


    here is the output:



    running initialisation block
    running initialisation block
    running initialisation block
    0
    300
    600
    900
    1200
    1500
    1800
    2100
    2400
    2700
    0
    300
    600
    900
    1200
    1500
    1800
    2100
    2400
    2700


    The static blocks were executed serially in the order in which they were declared and the values assigned by the first two static blocks is replaced by the final (third static block).



    Also one more thing to observe is that the static initialization block(s) ran only once i.e when the class was loaded by the JVM independent of how many objects were created.






    share|improve this answer






























      up vote
      4
      down vote













      You can define multiple static blocks. But I don't think it is really necessary. But if you will define, then they will be executed sequentially. i mean the static block defined first will execute first and the next block will execute next.






      share|improve this answer




























        up vote
        4
        down vote













        Yes. It is possible to define multiple static blocks in a java class. It helps in modularization of your initialization code, which in turn helps in better understanding and readable nature of the code(As peter mentioned).






        share|improve this answer





















          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10011340%2fcan-i-define-multiple-static-blocks%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          22
          down vote



          accepted










          yes, you can also make multiple initialisation blocks.



          This allows you to place code with the thing initialised.



          private static final Map<String, String> map;
          static {
          // complex code to initialise map
          }

          private static final DbConnection conn;
          static {
          // handle any exceptions and initialise conn
          }





          share|improve this answer

























            up vote
            22
            down vote



            accepted










            yes, you can also make multiple initialisation blocks.



            This allows you to place code with the thing initialised.



            private static final Map<String, String> map;
            static {
            // complex code to initialise map
            }

            private static final DbConnection conn;
            static {
            // handle any exceptions and initialise conn
            }





            share|improve this answer























              up vote
              22
              down vote



              accepted







              up vote
              22
              down vote



              accepted






              yes, you can also make multiple initialisation blocks.



              This allows you to place code with the thing initialised.



              private static final Map<String, String> map;
              static {
              // complex code to initialise map
              }

              private static final DbConnection conn;
              static {
              // handle any exceptions and initialise conn
              }





              share|improve this answer












              yes, you can also make multiple initialisation blocks.



              This allows you to place code with the thing initialised.



              private static final Map<String, String> map;
              static {
              // complex code to initialise map
              }

              private static final DbConnection conn;
              static {
              // handle any exceptions and initialise conn
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Apr 4 '12 at 12:47









              Peter Lawrey

              437k55551952




              437k55551952
























                  up vote
                  5
                  down vote













                  public class TryInitialisation {
                  static int values = new int[10];
                  static{
                  System.out.println("running initialisation block");
                  for (int i=0; i< values.length; i++)
                  values[i] = (int) (100.0 * i);
                  }
                  static{
                  System.out.println("running initialisation block");
                  for (int i=0; i< values.length; i++)
                  values[i] = (int) (200.0 * i);
                  }
                  static{
                  System.out.println("running initialisation block");
                  for (int i=0; i< values.length; i++)
                  values[i] = (int) (300.0 * i);
                  }
                  void listValues(){
                  for (int i=0; i<values.length; i++)
                  System.out.println(" " + values[i]);
                  }
                  public static void main(String args) {

                  TryInitialisation example = new TryInitialisation();
                  example.listValues();
                  example = new TryInitialisation(); // referencing a new object of same type
                  example.listValues();
                  }

                  }


                  here is the output:



                  running initialisation block
                  running initialisation block
                  running initialisation block
                  0
                  300
                  600
                  900
                  1200
                  1500
                  1800
                  2100
                  2400
                  2700
                  0
                  300
                  600
                  900
                  1200
                  1500
                  1800
                  2100
                  2400
                  2700


                  The static blocks were executed serially in the order in which they were declared and the values assigned by the first two static blocks is replaced by the final (third static block).



                  Also one more thing to observe is that the static initialization block(s) ran only once i.e when the class was loaded by the JVM independent of how many objects were created.






                  share|improve this answer



























                    up vote
                    5
                    down vote













                    public class TryInitialisation {
                    static int values = new int[10];
                    static{
                    System.out.println("running initialisation block");
                    for (int i=0; i< values.length; i++)
                    values[i] = (int) (100.0 * i);
                    }
                    static{
                    System.out.println("running initialisation block");
                    for (int i=0; i< values.length; i++)
                    values[i] = (int) (200.0 * i);
                    }
                    static{
                    System.out.println("running initialisation block");
                    for (int i=0; i< values.length; i++)
                    values[i] = (int) (300.0 * i);
                    }
                    void listValues(){
                    for (int i=0; i<values.length; i++)
                    System.out.println(" " + values[i]);
                    }
                    public static void main(String args) {

                    TryInitialisation example = new TryInitialisation();
                    example.listValues();
                    example = new TryInitialisation(); // referencing a new object of same type
                    example.listValues();
                    }

                    }


                    here is the output:



                    running initialisation block
                    running initialisation block
                    running initialisation block
                    0
                    300
                    600
                    900
                    1200
                    1500
                    1800
                    2100
                    2400
                    2700
                    0
                    300
                    600
                    900
                    1200
                    1500
                    1800
                    2100
                    2400
                    2700


                    The static blocks were executed serially in the order in which they were declared and the values assigned by the first two static blocks is replaced by the final (third static block).



                    Also one more thing to observe is that the static initialization block(s) ran only once i.e when the class was loaded by the JVM independent of how many objects were created.






                    share|improve this answer

























                      up vote
                      5
                      down vote










                      up vote
                      5
                      down vote









                      public class TryInitialisation {
                      static int values = new int[10];
                      static{
                      System.out.println("running initialisation block");
                      for (int i=0; i< values.length; i++)
                      values[i] = (int) (100.0 * i);
                      }
                      static{
                      System.out.println("running initialisation block");
                      for (int i=0; i< values.length; i++)
                      values[i] = (int) (200.0 * i);
                      }
                      static{
                      System.out.println("running initialisation block");
                      for (int i=0; i< values.length; i++)
                      values[i] = (int) (300.0 * i);
                      }
                      void listValues(){
                      for (int i=0; i<values.length; i++)
                      System.out.println(" " + values[i]);
                      }
                      public static void main(String args) {

                      TryInitialisation example = new TryInitialisation();
                      example.listValues();
                      example = new TryInitialisation(); // referencing a new object of same type
                      example.listValues();
                      }

                      }


                      here is the output:



                      running initialisation block
                      running initialisation block
                      running initialisation block
                      0
                      300
                      600
                      900
                      1200
                      1500
                      1800
                      2100
                      2400
                      2700
                      0
                      300
                      600
                      900
                      1200
                      1500
                      1800
                      2100
                      2400
                      2700


                      The static blocks were executed serially in the order in which they were declared and the values assigned by the first two static blocks is replaced by the final (third static block).



                      Also one more thing to observe is that the static initialization block(s) ran only once i.e when the class was loaded by the JVM independent of how many objects were created.






                      share|improve this answer














                      public class TryInitialisation {
                      static int values = new int[10];
                      static{
                      System.out.println("running initialisation block");
                      for (int i=0; i< values.length; i++)
                      values[i] = (int) (100.0 * i);
                      }
                      static{
                      System.out.println("running initialisation block");
                      for (int i=0; i< values.length; i++)
                      values[i] = (int) (200.0 * i);
                      }
                      static{
                      System.out.println("running initialisation block");
                      for (int i=0; i< values.length; i++)
                      values[i] = (int) (300.0 * i);
                      }
                      void listValues(){
                      for (int i=0; i<values.length; i++)
                      System.out.println(" " + values[i]);
                      }
                      public static void main(String args) {

                      TryInitialisation example = new TryInitialisation();
                      example.listValues();
                      example = new TryInitialisation(); // referencing a new object of same type
                      example.listValues();
                      }

                      }


                      here is the output:



                      running initialisation block
                      running initialisation block
                      running initialisation block
                      0
                      300
                      600
                      900
                      1200
                      1500
                      1800
                      2100
                      2400
                      2700
                      0
                      300
                      600
                      900
                      1200
                      1500
                      1800
                      2100
                      2400
                      2700


                      The static blocks were executed serially in the order in which they were declared and the values assigned by the first two static blocks is replaced by the final (third static block).



                      Also one more thing to observe is that the static initialization block(s) ran only once i.e when the class was loaded by the JVM independent of how many objects were created.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 11 at 8:04









                      rimalonfire

                      181117




                      181117










                      answered Jun 18 '14 at 8:29









                      dresh

                      284814




                      284814






















                          up vote
                          4
                          down vote













                          You can define multiple static blocks. But I don't think it is really necessary. But if you will define, then they will be executed sequentially. i mean the static block defined first will execute first and the next block will execute next.






                          share|improve this answer

























                            up vote
                            4
                            down vote













                            You can define multiple static blocks. But I don't think it is really necessary. But if you will define, then they will be executed sequentially. i mean the static block defined first will execute first and the next block will execute next.






                            share|improve this answer























                              up vote
                              4
                              down vote










                              up vote
                              4
                              down vote









                              You can define multiple static blocks. But I don't think it is really necessary. But if you will define, then they will be executed sequentially. i mean the static block defined first will execute first and the next block will execute next.






                              share|improve this answer












                              You can define multiple static blocks. But I don't think it is really necessary. But if you will define, then they will be executed sequentially. i mean the static block defined first will execute first and the next block will execute next.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Apr 4 '12 at 12:45









                              Chandra Sekhar

                              12k115691




                              12k115691






















                                  up vote
                                  4
                                  down vote













                                  Yes. It is possible to define multiple static blocks in a java class. It helps in modularization of your initialization code, which in turn helps in better understanding and readable nature of the code(As peter mentioned).






                                  share|improve this answer

























                                    up vote
                                    4
                                    down vote













                                    Yes. It is possible to define multiple static blocks in a java class. It helps in modularization of your initialization code, which in turn helps in better understanding and readable nature of the code(As peter mentioned).






                                    share|improve this answer























                                      up vote
                                      4
                                      down vote










                                      up vote
                                      4
                                      down vote









                                      Yes. It is possible to define multiple static blocks in a java class. It helps in modularization of your initialization code, which in turn helps in better understanding and readable nature of the code(As peter mentioned).






                                      share|improve this answer












                                      Yes. It is possible to define multiple static blocks in a java class. It helps in modularization of your initialization code, which in turn helps in better understanding and readable nature of the code(As peter mentioned).







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Apr 4 '12 at 12:57









                                      siva.pcu

                                      411




                                      411






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10011340%2fcan-i-define-multiple-static-blocks%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          List item for chat from Array inside array React Native

                                          Thiostrepton

                                          Caerphilly