Create---Single-instance Singleton

Source: Internet
Author: User

    • Code:
1  Packagecom.design;2 3 /**4 * Test Class5  * @authorAdministrator6  *7  */8  Public classTest {9     /**Ten * Main function One      * @paramargs A      */ -      Public Static voidMain (string[] args) { -Singleton First_inst =singleton.getinstance (); theFirst_inst.setstring ("8288282"); -          -Singleton Sencond_inst =singleton.getinstance (); -System.out.println ("Sencond_inst:" +sencond_inst.getstring ()); +Sencond_inst.setstring ("I am a single instance"); -          +Singleton Third_inst =singleton.getinstance (); ASystem.out.println ("Third_inst:" +third_inst.getstring ()); at     } - } -  - /** - * Single Instance -  * @authorAdministrator in  * -  */ to classSingleton + { -     /** the * Get Instance *      * @return $      */Panax Notoginseng      Public StaticSingleton getinstance () -     { the         returnMyinst; +     } A      the     /** + * Get String -      * @return $      */ $      PublicString getString () -     { -         returnstr; the     } -     Wuyi     /** the * Set String -      * @paramStr Wu      */ -      Public voidsetString (String str) About     { $          This. str =str; -     } -      -     /** A * Create an instance +      */ the     Private StaticSingleton Myinst =NewSingleton (); -      $     /** the * Test Code the      */ the     PrivateString str; the      -     /** in * Privatization Constructor the      */ the     PrivateSingleton () About     { thestr = ""; the     } the}

    • Results

sencond_inst:8288282
Third_inst: I am a single instance

Create---Single-instance Singleton

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.