Single-Case mode

Source: Internet
Author: User

Package single case mode;

public class Test01 {

private String name;

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Private Test01 () {
Private construction methods
}
Create your own objects
private static Test01 Ts=null;

External access Methods
public static Test01 Getprint () {
object is created if the object is empty, otherwise this object is returned
if (ts==null) {

Ts=new Test01 ();
}

return TS;
}

public void Printinfo () {

System.out.println ("outmsg:" +name);
}

public static void Main (String[]args) {

Test01 Ts1=test01.getprint ();
Ts1.setname ("Zhang San");
System.out.println (Ts1.getname ());

Test01 Ts2=test01.getprint ();
Ts2.setname ("John Doe");
System.out.println (Ts2.getname ());

Ts1.printinfo ();
Ts2.printinfo ();


if (ts1==ts2) {

System.out.println ("same instance");
}else if (ts1!=ts2) {

System.out.println ("different instances");
}else{

SYSTEM.OUT.PRINTLN ("error");
}

}
}

Output Result:

Tom
John doe
OUTMSG: John Doe
OUTMSG: John Doe
Same instance

Single-Case mode

Related Article

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.