1 2 Previous Next 24 Replies Latest reply: Feb 10, 2006 8:38 AM by 843833 Go to original post RSS
      • 15. Re: Help required in understanding of static blocks in java
        843833
        i want to know more about "static"
        i want to call static block of 1st class from another class.
        now i got the answer....
        this is my first class:

        class STAT
        {
             static{
                  System.out.println("hello world");
                  for(int i=1;i<=10;i++)
                  {
                       System.out.println(i);
                  }
                  
             }
        }


        now i want to call this static block from another class
        so here is the code:

        class STAT_call{
             static {
                       try{               
                            Class stat=     Class.forName("STAT");
                            }catch(Exception e){
                                 System.err.println(e);
                                 }

                       }     
             }

        the value will be printed prsent in class STAT by executing this class.
        regards
        -Rohit
        • 16. Re: Help required in understanding of static blocks in java
          843833
          class STAT {
               static {
               System.out.println("hello world");
               for (int i = 1; i <= 10; i++) {
                    System.out.println(i);
                    }

               }
          }

          class STAT_call {
               
               public static void main(String[] args){
               STAT stat = new STAT(); // all u have
          //to do is instantiate the
          //STAT class
                    
               }
          }



          as soon as the STAT class is instantiated, its static block will be executed.
          In case your Static_call class is extending STAT class, you dont even need to instantiate it, just creating a reference of type STA will do the trick.
          • 17. Re: Help required in understanding of static blocks in java
            843833
            i know that thing, i was implementing the two classes without main() method.....
            just check out the cod again. and also i dnt want to create any object in it.
            regards
            Rohit
            • 18. Synchronization of static initializer blocks
              843833
              Hello!
              Can someone explains me if there's the possibility of synchronizing static initializer blocks?
              I illustrate a possible scenario:
              - a class with a static attribute corresponding to itself is extended by another class;
              - these two classes can be referred ( and so created because I call in the initialization static block the constructor) everywhere by concurrent threads

              If a thread refers the class (sub or super) while another thread refers the same class (or its subclass or its superclass), I think that concurrent executions of the static block may be performed.

              It's true or static initializer blocks own an implicit class synchronization?
              If it's true can someone indicate me a possible way to avoid this behaviour?
              • 19. Re: Synchronization of static initializer blocks
                jduprez
                Hello!
                Can someone explains me if there's the possibility of
                synchronizing static initializer blocks?
                If you need to synchronize some code within a static block, you can explicitly synchronize on some static member (or on the class object); but you won't need it if your only concern is the following:
                I illustrate a possible scenario:
                - a class with a static attribute corresponding to
                o itself is extended by another class;
                If a thread refers the class (sub or super) while
                another thread refers the same class (or its subclass
                or its superclass), I think that concurrent
                executions of the static block may be performed.
                I don't think so.
                The static initializer is run only once, during class initialization, which is one step during class loading. The JVM makes sure class initialization is thread-safe (I guess under the hood it synchronizes on the Class object), so your scenario seems impossible.

                I remember reading this somewhere, possibly in the JVM specification, not sure...
                • 20. Re: Synchronization of static initializer blocks
                  843833
                  Thank you very much! What you say is correct! Before your reply I searched some information and I tried the response you gave me.
                  Initialization of static blocks is performed once (and this is obvious); otherwise JVM avoids concurrent initializations.

                  Thank you... and have a nice day!
                  • 21. Re: Synchronization of static initializer blocks
                    jschellSomeoneStoleMyAlias
                    - these two classes can be referred ( and so created
                    d because I call in the initialization static block
                    the constructor) everywhere by concurrent threads
                    That doesn't sound like a particularily good idea. At the very least it is likely to confuse either you are someone who has to maintain your code later on. So hopefully you are providing plenty of comments as to why you need to do it exactly that way.
                    - a class with a static attribute corresponding to itself is extended by another class;
                    If this matters then you do have a problem. Everything you discussed here that occurs in the parent is done before the child does anything.
                    • 22. Re: Help required in understanding of static blocks in java
                      843833
                      You cannot do that. However when ever you load a class, it gets called automatically.
                      for e.g.
                      static block of below class
                      class Class1 {
                          static {
                              System.out.println("Loading Class1");
                          }
                      }
                      can be executed with following code
                      try {
                          Class.forName("Class1");
                      }
                      catch(ClassNotFoundException ex) {
                          ex.printStackTrace();
                      }
                      • 23. Help required understanding the purpose of having instance Init blocks
                        843833
                        I want to know exactly what the purpose of having instance initialization blocks in Java. As in static init blocks, we can execute some commands before creating an instance of a class.

                        And also in instance init blocks, we are executing some commands which we can do in a constructor. So I’m little bit confused of the purpose of having this concept instance init blocks without writing it in the constructor itself.

                        Really appreciate if anyone can explain this ASAP as I am still a beginner who is studying for SCJP exam.

                        Thanks in advance.
                        • 24. Re: Help required in understanding of static blocks in java
                          843833
                          Hi,

                          Static blocks are also called static initializers in a Java.
                          These are code statements that are executed once when the class is loaded.

                          Will provide you with an example.

                          static {
                          int c = a + b;

                          }
                          The code above will be executed once when the class is loaded regardless of how many instances are created
                          in the class.
                          Unlike instance initializers which are exectuted whenever an instance of a class is created.

                          Will provide you with an example.
                          {
                          c = a + b;
                          }



                          Cheers,
                          1 2 Previous Next