fork download
  1. class PrintServerEager{
  2. private static PrintServerEager instance1 = new PrintServerEager();
  3. private PrintServerEager(){
  4. }
  5. public static PrintServerEager getInstance1()
  6. {
  7. return instance1;
  8. }
  9. void printDocument1(String documentName)
  10. {
  11. System.out.println("Name of the document is = "+documentName);
  12. }
  13. }
  14.  
  15. class PrintServerLazy{
  16. private static PrintServerLazy instance2;
  17. private PrintServerLazy(){
  18. }
  19. public static PrintServerLazy getInstance2()
  20. {
  21. if(instance2 == null)
  22. {
  23. instance2 = new PrintServerLazy();
  24. }
  25. return instance2;
  26. }
  27. void printDocument2(String documentName)
  28. {
  29. System.out.println("Name of the document is = "+documentName);
  30. }
  31. }
  32.  
  33. public class Main{
  34. public static void main (String[] args) {
  35. PrintServerEager obj1 = PrintServerEager.getInstance1();
  36. PrintServerEager obj2 = PrintServerEager.getInstance1();
  37.  
  38. obj1.printDocument1("Report.pdf");
  39. obj2.printDocument1("Assignment.docx");
  40.  
  41. if(obj1 == obj2){
  42. System.out.println("Both references point to the same Eager instance");
  43. }
  44. else{
  45. System.out.println("Get out!");
  46. }
  47.  
  48. PrintServerLazy obj3 = PrintServerLazy.getInstance2();
  49. PrintServerLazy obj4 = PrintServerLazy.getInstance2();
  50.  
  51. obj3.printDocument2("Thesis.pdf");
  52. obj4.printDocument2("LabReport.docx");
  53.  
  54. if(obj3==obj4)
  55. {
  56. System.out.println("Both references point to the same Lazy instance");
  57. }
  58. else{
  59. System.out.println("Get out too!");
  60. }
  61. }
  62. }
Success #stdin #stdout 0.12s 57312KB
stdin
Standard input is empty
stdout
Name of the document is = Report.pdf
Name of the document is = Assignment.docx
Both references point to the same Eager instance
Name of the document is = Thesis.pdf
Name of the document is = LabReport.docx
Both references point to the same Lazy instance