When a class is loaded, is its inner class loaded at the same time? Static internal class single case mode _ design pattern

Source: Internet
Author: User
Tags static class
When a class is loaded, its inner class is loaded at the same time. Now let's do an experiment to see.
Java Code    public class outer {       static {            SYSTEM.OUT.PRINTLN ("Load outer class ...");        }              / /static internal class        static class StaticInner {            static {                system.out.println ("Load static inner class ...");            }                       static void staticinnermethod ()  {                SYSTEM.OUT.PRINTLN ("static  Inner meThod ");           }       }                   public  Static void main (String[] args)  {            outer outer = new outer ()       //whether its inner class is also loaded at the moment.             system.out.println ("=========== Split line ========== = ");           outer.staticinner.staticinnermethod ();       //static methods for calling internal classes        }  }  
Run Result:
Load Outer class ...
========== Split Line ==========
Load static inner class ...
Static Inner Method ...

When the constructor method is invoked, the outer class outer is loaded, but its static inner class Staticinner is not loaded. Staticinner is not loaded until the static method that calls the inner class (below the split line) is invoked. A similar experiment can be done to verify the situation of non-static internal classes.
Conclusion: When a class is loaded, its inner classes are not loaded at the same time. A class is loaded and occurs only if one of its static members (static domain, constructor, static method, and so on) is invoked.

Depending on the fact that the inner class does not load at the same time as its outer class is loaded, we can extend one way of implementing a single example pattern:
Java code public class Singleton {private Singleton () {} static class Singletonholder {Priva       Te static final Singleton instance = new Singleton ();       public static Singleton getinstance () {return singletonholder.instance; }   }
The implementation method is simpler, and it realizes the lazy initialization (lazy-initialazation) guaranteed by the aforementioned facts, and the JVM guarantees the correctness of concurrent access.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.