Java Singleton mode

Source: Internet
Author: User

1  PackageCOM.HANQI.Z1P1;2 3 //Singleton mode: Single Instance4  Public classTest0521 {5 6     //implementation, only one instance is generated in memory7     8     //Save resources and security9     Ten     //implementation steps: One     //1. Hide the construction method, do not allow direct new A     //turn the construction method into private -     PrivateTest0521 () -     { the          -     } -      -     //3. Prepare an instance +     Private StaticTest0521 tt=NewTest0521 (); -      +      A     //2. Develop a method that can obtain a unique instance at     //provides a static method that returns the prepared instance -      Public StaticTest0521 instance () -     { -         returntt; -          -          in     } -      to     Private intM=0; +      -      Public voidRun () the     { *          for(inti=0;i<10;i++) $         {Panax Notoginsengm++; -              the              +System.out.println ("m=" +m); A         } the     } +      -      $      $      -}
1         // Test Singleton mode 2         3         System.out.println ("T6: Single Case"); 4         Test0521 t6=test0521.instance (); 5         T6.run (); 6         System.out.println ("T7: Single Case"); 7         Test0521 t7=test0521.instance (); 8         T7.run ();

Test results:

Java Singleton mode

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.