1 2 Previous Next 24 Replies Latest reply: Feb 10, 2006 8:38 AM by 843833 RSS

    Help required in understanding of static blocks in java

    843833
      Hi ,
      Can some one help me in understanding the difference between static blocks in java....also what is the difference in declaring some static variables in a class and and declaring some variables in a static block? From an architecture viewpoint when should one use static blocks in Java?
        • 1. Re: Help required in understanding of static blocks in java
          843833
          Static blocks are piece of code that can be executed before creating an instance of a class. For example whatever you include in the main method will be executed without you having to create the instance of the class using the new operator. So you can say the main method is a static block. You can use a static block when you want to do some processing without creating an instance of a class. A static variable is a membor of a class, not for each instance of the class. Use a static variable when you want to make sure that the value is consistent when used by other objects.
          • 2. Re: Help required in understanding of static blocks in java
            843833
            Static blocks are piece of code that can be executed
            before creating an instance of a class.
            static blocks are executed once, when the class itself is loaded by the JVM. They are not executed before creating each instance of a class.

            > For example whatever you include in the main method will be
            executed without you having to create the instance of
            the class using the new operator. So you can say the
            main method is a static block.
            main is not a static initialisation block but a static method. a special case static method at that - it is only executed when the containing class is specified as a parameter to the JVM. (unless you specifcally call it elsewhere in code - but that would be bad form).

            in answer to the original post, static variables belong to the class. each instance of the class share the same static variables. Public static vars can be accessed by prefixing them with the class name. A static initialisation block can be used to initialise static variables. Variables declared within the static initialisation block exist only within the scope of the block.

            e.g.
            public class Foo {
             
                static Bar bar;        // static member variable
            
                  // static initialisation block
                static {
                     // variable declared in static block...
                    String barInfo = System.getParameter("barInfo");
            
                    // ... used to initialise the static var
                   bar = new Bar(barInfo);
               }
            }
            • 3. Re: Help required in understanding of static blocks in java
              843833
              Static blocks are piece of code that can be
              executed
              before creating an instance of a class.
              static blocks are executed once, when the class
              itself is loaded by the JVM. They are not executed
              before creating each instance of a class.
              For example whatever you include in the main
              n method will be
              executed without you having to create the instance
              of
              the class using the new operator. So you can say
              the
              main method is a static block.
              main is not a static initialisation block but a
              static method. a special case static method at that -
              it is only executed when the containing class is
              specified as a parameter to the JVM. (unless you
              specifcally call it elsewhere in code - but that
              would be bad form).

              in answer to the original post, static variables
              belong to the class. each instance of the class share
              the same static variables. Public static vars can be
              accessed by prefixing them with the class name. A
              static initialisation block can be used to
              initialise static variables. Variables declared
              within the static initialisation block exist only
              within the scope of the block.

              e.g.
              public class Foo {
              
              static Bar bar;        // static member variable
              
              // static initialisation block
              static {
              // variable declared in static block...
              String barInfo =
              arInfo = System.getParameter("barInfo");
              
              // ... used to initialise the static var
              bar = new Bar(barInfo);
              }
              }
              So is the only purpose of static initialization blocks is to initialize static variables? Does the initialization of static variables inside a static block make any difference in performance? If yes , then how ?
              • 4. Re: Help required in understanding of static blocks in java
                843833
                So is the only purpose of static initialization
                blocks is to initialize static variables?
                well it's probably the most common use. the name sort of suggests it.
                Does the
                initialization of static variables inside a static
                block make any difference in performance? If yes ,
                then how ?
                no it's not related to performance. it's just a clean way to set up static
                variables.
                • 5. Re: Help required in understanding of static blocks in java
                  843833
                  Hi, one more question regardiong the static block.
                  if the scope of a static block variable is only within that block, how is that variable going to be useful to me? (exceot for pre-processing before call of main() )
                  and, is there any way i can call a variable outside the

                  static { int i; }

                  block?
                  • 6. Re: Help required in understanding of static blocks in java
                    843833
                    you use the variable within the block to derive some value / hold a temporary etc/
                    Just like a local variable in a method. And no, you can't access the variable outside it's scope - that goes for any variable, not just those in static blocks. If you need to do this then the variable should be a static member of your class.
                    • 7. Re: Help required in understanding of static blocks in java
                      843833
                      I have 2 questions regarding static initialisation blocks, mostly just to clarify my understanding while this thread is active:

                      1: is this:
                      private static String name = new String("bob");
                      exactly the same programaticaly as:
                      private static String name;
                      static {
                          name = new String("bob");
                      }
                      2: is the use of static initialisation blocks generally conisderd "bad form". Issues i could see are:
                      * increacing loading time of aplication
                      * reducing readability (as not everyone even knows you can do this).

                      thanks,
                      Ken
                      • 8. Re: Help required in understanding of static blocks in java
                        843833
                        1: is this:
                        private static String name = new String("bob");
                        exactly the same programaticaly as:
                        private static String name;
                        static {
                        name = new String("bob");
                        }
                        yes. in fact the compiler will probably emit the same byte code for it.

                        >
                        2: is the use of static initialisation blocks
                        generally conisderd "bad form". Issues i could see
                        are:
                        * increacing loading time of aplication
                        well if you have to initialise you have to do it somewhere. Static init blocks are not necessarily run on startup, but when a class is loaded. If you determine that it's causing a performance issue, then you have to look at it.
                        You can't just say that "static init blocks are poor for performance".
                        * reducing readability (as not everyone even knows
                        s you can do this).

                        thanks,
                        Ken
                        mmm. Don't know how to answer this - should you not use a feature of a language because some coders won't know it straight away ? Won't they
                        pick up a book to find out ? If not we'd all code in BASIC.
                        • 9. Re: Help required in understanding of static blocks in java
                          843833
                          yes. in fact the compiler will probably emit the same  byte code for it. 
                          sorry messed up the code tags there
                          • 10. Re: Help required in understanding of static blocks in java
                            843833
                            Thanks for your responce, thats exactly what I wanted to know.

                            The whole readability v. using language functions is a long standing one - I haven't fully decided what side of it I am on, but either way, this isn't the place to discuss that :)

                            thanks again,
                            Ken
                            • 11. Re: Help required in understanding of static blocks in java
                              843833
                              hi David
                              i want to know more about "static"
                              i want to call static block of 1st class from another class.
                              class 1
                              class EX1{
                              static {
                              System.out.println("i m in class EX1");
                              }
                              }

                              class 2
                              class EX2{
                              ////what code should i add to this class to call the static block of class 1

                              }

                              regards
                              Rohit
                              • 12. Re: Help required in understanding of static blocks in java
                                843833
                                hi David
                                i want to know more about "static"
                                i want to call static block of 1st class from another class.
                                class 1
                                class EX1{
                                static {
                                System.out.println("i m in class EX1");
                                }
                                }

                                class 2
                                class EX2{
                                ////what code should i add to this class to call the static block of class 1

                                }

                                regards
                                Rohit
                                • 13. Help required in understanding of static blocks in java
                                  843833
                                  hi David
                                  i want to know more about "static"
                                  i want to call static block of 1st class from another class.
                                  class 1
                                  class EX1{
                                  static {
                                  System.out.println("i m in class EX1");
                                  }
                                  }

                                  class 2
                                  class EX2{
                                  ////what code should i add to this class to call the static block of class 1

                                  }

                                  regards
                                  Rohit
                                  • 14. Re: Help required in understanding of static blocks in java
                                    843833
                                    You can't do this I'm afraid.

                                    If you want to call static code in another class it'll have to be in a public static method. But note that you can't call static methods from a static init block so you won't be able to move the code in your static block to a static method in order to share it.
                                    1 2 Previous Next