1 2 Previous Next 15 Replies Latest reply on Aug 18, 2009 6:58 PM by jschellSomeoneStoleMyAlias

    Static class

    807580
      when we are declaring an inner class with static key word what does it meant.why cant we use static modifier in case of normal class.what is the advantage we r getting in terms of performance when we r declaring an Inner class as static.I know in case of static variable,That belong to Class,All instances share the same copy,And in case of static methods we can refer that using Class name itself.
      public class Test {
           static class MyTest{
                          public void display(){
                               System.out.println("MyTest");
                     }
           }
           public static void main(String[] args) {
                try{
                     MyTest c=new MyTest();
                     MyTest c1=new MyTest();
                     System.out.println(c.hashCode()+"###"+c1.hashCode());
                }
                catch(Exception exp){
                     System.out.println(exp.toString());
                }
           }
      }

      1) why can't i declare Test as a static class like MyTest.

      2) when i am declaring MyTest as static(static class MyTest) what performance advantage we r getting.Can any one give me a better Explanation on this.
      thanks in advance.
        • 1. Re: Static class
          JoachimSauer
          1.) Performance is irrelevant in the static-vs.-non-static decision. There are many, many much more important aspects in this decision.
          2.) a static inner class is one that can exist independent of an instance of its outer class.
          3.) Since a top-level class has no "outer class" item 2.) is obviously not relevant for a top-level class so a "static" modifier makes no sense here

          If you're comming from a .NET world, then please not that a "static class" is something very different in .NET/C#: It's a class that can't be instanciated and has only static members. This can be done in Java as well, but it has no explicit language support and it's usually not called a "static class" here.
          • 2. Re: Static class
            807580
            1.) All instances of non-static member classes have access to the surrounding class's attributes and static final variables, whereas static member classes can only access the static members of the outer class (methods and variables).
            2.) Static member classes are NOT referred to as inner classes.
            3.) Instances of an inner class class (non-static member class) "live" inside an instance of the outer class (and can't exist without an instance of the outer class). Iterators e.g. are implemented as inner classes.
            4.) You can also declare static member interfaces.
            • 3. Re: Static class
              807580
              Here one very good explanation:

              [http://blogs.sun.com/darcy/entry/nested_inner_member_and_top]

              Edited by: Trollhorn on Aug 17, 2009 7:22 AM
              • 4. Re: Static class
                796440
                Paul_Erric wrote:
                why cant we use static modifier in case of normal class
                Because static means "associated with the enclosing class as a whole." In the case of a top-level class, there is no enclosing class, so static would be meaningless.
                what is the advantage we r getting in terms of performance when we r declaring an Inner class as static
                None.
                • 5. Re: Static class
                  807580
                  There is one very slightly performance benefit of using a static member class... first of all, the construction is slightly faster, because you don't need to create a reference to the enclosing instance... then, the reference itself is a waste of memory (if you don't need access to the surrounding classes attributes)... consider e.g. a Tree like TreeMap... the Node class is declared static because the node does not need access to the Tree's attributes... if the Tree has lots of nodes, it may make a difference...
                  • 6. Re: Static class
                    807580
                    See chapter 22 in Effective Java: Favor static member classes over non-static
                    • 7. Re: Static class
                      807580
                      Also an instance of the main class can't be garbage collected if any instances of a non-static inner class are reachable that relate to it. Not often a factor but it could be.

                      Probably best to start off your inner classes as static until you come across a reason for accessing the surrounding class from them.
                      • 8. Re: Static class
                        796440
                        malcolmmc wrote:
                        Probably best to start off your inner classes as static until you come across a reason for accessing the surrounding class from them.
                        Enclosing instance. The enclosing class is available whether the nested class is static or not.

                        </pb>
                        • 9. Re: Static class
                          807580
                          Some interesting thoughts you all have. One thing I haven't seen anyone mention yet, which sometimes crosses my mind, is this: does the nested class (or an instance of it) make any sense outside of the enclosing class? I often consider this as well when attempting to decide whether a nested class should be static or not.
                          • 10. Re: Static class
                            DrClap
                            Skotty wrote:
                            One thing I haven't seen anyone mention yet, which sometimes crosses my mind, is this: does the nested class (or an instance of it) make any sense outside of the enclosing class? I often consider this as well when attempting to decide whether a nested class should be static or not.
                            When you're deciding whether or not to make a class-level variable static, you don't take into consideration whether it could be used outside its declaring class. Likewise that usability is irrelevant when deciding to make a nested class static.
                            • 11. Re: Static class
                              796440
                              Skotty wrote:
                              Some interesting thoughts you all have. One thing I haven't seen anyone mention yet, which sometimes crosses my mind, is this: does the nested class (or an instance of it) make any sense outside of the enclosing class? I often consider this as well when attempting to decide whether a nested class should be static or not.
                              Static has nothing to do with whether it makes sense to use it outside the class. Static is only about whether it should be associated with the declaring class as a whole, vs. with individual instances.
                              • 12. Re: Static class
                                807580
                                jverd wrote:
                                Skotty wrote:
                                Some interesting thoughts you all have. One thing I haven't seen anyone mention yet, which sometimes crosses my mind, is this: does the nested class (or an instance of it) make any sense outside of the enclosing class? I often consider this as well when attempting to decide whether a nested class should be static or not.
                                Static has nothing to do with whether it makes sense to use it outside the class. Static is only about whether it should be associated with the declaring class as a whole, vs. with individual instances.
                                I guess I was thinking about it from the standpoint of whether outside classes could instantiate instances of the nested class (assuming they are declared public). But of course, if you didn't want that to happen, you would just declare it private. So nevermind.
                                • 13. Re: Static class
                                  796440
                                  Skotty wrote:
                                  jverd wrote:
                                  Skotty wrote:
                                  Some interesting thoughts you all have. One thing I haven't seen anyone mention yet, which sometimes crosses my mind, is this: does the nested class (or an instance of it) make any sense outside of the enclosing class? I often consider this as well when attempting to decide whether a nested class should be static or not.
                                  Static has nothing to do with whether it makes sense to use it outside the class. Static is only about whether it should be associated with the declaring class as a whole, vs. with individual instances.
                                  I guess I was thinking about it from the standpoint of whether outside classes could instantiate instances of the nested class
                                  Which again has nothing to do with whether it's static. If the nested class is public, and has a public c'tor, I can instantiate it, regardless of whether it's static or not.
                                  • 14. Re: Static class
                                    807580
                                    jverd wrote:
                                    Skotty wrote:
                                    jverd wrote:
                                    Skotty wrote:
                                    Some interesting thoughts you all have. One thing I haven't seen anyone mention yet, which sometimes crosses my mind, is this: does the nested class (or an instance of it) make any sense outside of the enclosing class? I often consider this as well when attempting to decide whether a nested class should be static or not.
                                    Static has nothing to do with whether it makes sense to use it outside the class. Static is only about whether it should be associated with the declaring class as a whole, vs. with individual instances.
                                    I guess I was thinking about it from the standpoint of whether outside classes could instantiate instances of the nested class
                                    Which again has nothing to do with whether it's static. If the nested class is public, and has a public c'tor, I can instantiate it, regardless of whether it's static or not.
                                    Okay, I just learned something new. Based on your comment, I played around and discovered I could do this:
                                    public class Enclosing {
                                         public class Nested {
                                         }
                                    }
                                    ...
                                    Enclosing e = new Enclosing();
                                    Enclosing.Nested en = e.new Nested();
                                    Been programming in Java in and outside of work for many years and never knew you could do that. I assumed if there was such a syntax it would be "new e.Nested()".

                                    Edited by: Skotty on Aug 18, 2009 6:55 PM
                                    1 2 Previous Next