Why inner class does not have static block?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Why inner class does not have static block?

    Yesterday night i was learning inner class there i came across a thing that Inner Class does not support Static Block ...
    I need the explanation ..Please!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by dmjpro
    Yesterday night i was learning inner class there i came across a thing that Inner Class does not support Static Block ...
    I need the explanation ..Please!
    Inner classes don't need static blocks because they can use the static blocks
    of the outer class that embraces them.

    kind regards,

    Jos

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by JosAH
      Inner classes don't need static blocks because they can use the static blocks
      of the outer class that embraces them.

      kind regards,

      Jos
      Thanks :-)
      That means if inner class loaded then enclosing class gets loaded.
      But this code snippet is not working properly, means outer class static block is not running.
      [code=java]
      class OuterClass{
      static{
      System.out.prin tln("Hello ...!!!!!");
      }
      static class InnerCLass{

      }
      }

      public class StaticTest {

      /** Creates a new instance of StaticTest */
      public StaticTest() {
      }

      public static void main(String args[]){
      OuterClass.Inne rCLass ic = new OuterClass.Inne rCLass();
      }
      }
      [/code]

      Please explain ...!

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by dmjpro
        Thanks :-)
        That means if inner class loaded then enclosing class gets loaded.
        But this code snippet is not working properly, means outer class static block is not running.
        [code=java]
        class OuterClass{
        static{
        System.out.prin tln("Hello ...!!!!!");
        }
        static class InnerCLass{

        }
        }

        public class StaticTest {

        /** Creates a new instance of StaticTest */
        public StaticTest() {
        }

        public static void main(String args[]){
        OuterClass.Inne rCLass ic = new OuterClass.Inne rCLass();
        }
        }
        [/code]

        Please explain ...!
        Sorry Josh when I ran this file then most probably it was not complied ..previous started running ....Sorry!
        Anyway Thanks Josh!!!

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by dmjpro
          Thanks :-)
          That means if inner class loaded then enclosing class gets loaded.
          But this code snippet is not working properly, means outer class static block is not running.
          [code=java]
          class OuterClass{
          static{
          System.out.prin tln("Hello ...!!!!!");
          }
          static class InnerCLass{

          }
          }

          public class StaticTest {

          /** Creates a new instance of StaticTest */
          public StaticTest() {
          }

          public static void main(String args[]){
          OuterClass.Inne rCLass ic = new OuterClass.Inne rCLass();
          }
          }
          [/code]

          Please explain ...!
          That class is not an inner class; it's just a static nested class. If you'd leave out
          the 'static' it would've been an inner class.

          kind regards,

          Jos

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by JosAH
            That class is not an inner class; it's just a static nested class. If you'd leave out
            the 'static' it would've been an inner class.

            kind regards,

            Jos
            I think the same thing happens to static nested class and inner class. :-)

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by dmjpro
              I think the same thing happens to static nested class and inner class. :-)
              No, static nested classes can have static blocks unlike inner classes. A static
              nested class belongs to the outer class while an inner class belongs to an
              instantiation (an object) of the outer class.

              kind regards,

              Jos

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Originally posted by JosAH
                No, static nested classes can have static blocks unlike inner classes. A static
                nested class belongs to the outer class while an inner class belongs to an
                instantiation (an object) of the outer class.

                kind regards,

                Jos

                What would happen if Inner Class supports Static block?

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by dmjpro
                  What would happen if Inner Class supports Static block?
                  It doesn't support static blocks; an instantiation of an inner class belongs to an
                  instantiation of the outer class; like planets rotating around a star; the Earth would
                  be an instantiation of the inner class Planet which has an outer class Star and
                  an instantiation Sun; that makes an Earth rotate around a Sun. Everything static
                  to Earth would just be an element of the object Sun which isn't static but just a
                  member of that outer class. If you want something 'more static' you can use the
                  static members of the Star class; you don't need a separate static block for that.

                  kind regards,

                  Jos

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    Originally posted by JosAH
                    It doesn't support static blocks; an instantiation of an inner class belongs to an
                    instantiation of the outer class; like planets rotating around a star; the Earth would
                    be an instantiation of the inner class Planet which has an outer class Star and
                    an instantiation Sun; that makes an Earth rotate around a Sun. Everything static
                    to Earth would just be an element of the object Sun which isn't static but just a
                    member of that outer class. If you want something 'more static' you can use the
                    static members of the Star class; you don't need a separate static block for that.

                    kind regards,

                    Jos
                    It would be more helpful if you please explain this line ...
                    Everything static
                    to Earth would just be an element of the object Sun which isn't static but just a
                    member of that outer class

                    Sorry for inconvenience ..... :-(

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by dmjpro
                      It would be more helpful if you please explain this line ...
                      Everything static
                      to Earth would just be an element of the object Sun which isn't static but just a
                      member of that outer class

                      Sorry for inconvenience ..... :-(
                      It was an analogy: whatever seems static for Earth, Mars, Venus, Mercury or
                      whatever planet rotating around the Sun would simply be an instance member
                      of that Sun object; which in turn would simply be an *instance* of the Star class.

                      Of course static members of the Star class can be freely used by instances of
                      the Planet class.

                      kind regards,

                      Jos

                      Comment

                      Working...