Question: How to restrict a class from creating multiple objects?

  • Restring a class from creating multiple objects or restricting class from creating not more than three objects this type of interview questions will come for experience interview or written tests.

  • Yes we can restrict a class from creating multiple objects.
  • As we know using singleton we can restrict a class from creating multiple objects it will create single object and share it.
  • Same design pattern we can apply here with counter 
  • In this we will take a static variable counter to check how many objects created
  • As we can access static variables in constructor and static variables are class level we can stake help of static variable to count number object created.
  • First three time we will create new objects and forth time we need to return 3rd object reference. if we don't want same object 4th time we can return null
  • By printing the hashcode() of the object we can check how many objects created. 
  •  

Basic java example program to restrict a class from not to create more than three instances




  1. package com.instanceofjava;
  2.  
  3. public class RestrictObjectCreation{

  4. private static RestrictObjectCreationobject;
  5. public static int objCount = 0;
  6.  
  7. private RestrictObjectCreation()
  8. {
  9.      System.out.println("Singleton(): Private constructor invoked");
  10.  
  11. objCount  ++;
  12. }
  13.  
  14. public static RestrictObjectCreation getInstance()
  15. {
  16.  
  17. if (objCount < 3)
  18. {
  19.  
  20. object = new RestrictObjectCreation();
  21.  
  22.  }
  23.  
  24. return object;
  25.  
  26. }
  27.  
  28. }
     

  1. package instanceofjava;
  2.  
  3. public class Test{

  4. public static void main(String args[]) {
  5.  
  6. RestrictObjectCreation obj1= RestrictObjectCreation.getInstance();
  7. RestrictObjectCreation obj2= RestrictObjectCreation.getInstance();
  8. RestrictObjectCreation obj3= RestrictObjectCreation.getInstance();
  9. RestrictObjectCreation obj4= RestrictObjectCreation.getInstance();
  10. RestrictObjectCreation obj5= RestrictObjectCreation.getInstance();
  11.  
  12. System.out.println(obj1.hashCode());
  13. System.out.println(obj2.hashCode());
  14. System.out.println(obj3.hashCode());
  15. System.out.println(obj4.hashCode());
  16. System.out.println(obj5.hashCode());
  17.  
  18. }
  19. }



Output:

  1. RestrictObjectCreation(): Private constructor invoked
  2. RestrictObjectCreation(): Private constructor invoked
  3. RestrictObjectCreation(): Private constructor invoked
  4. 705927765
  5. 366712642
  6. 1829164700
  7. 1829164700
  8. 1829164700


restrict  from creating multiple objects


You Might Like:

1.Five Different places to create object of a class




3.Six different ways to iterate list in java

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

2 comments for Java Example program to restrict a class from creating not more than three objects

  1. very good explanation sir.move on

    ReplyDelete
  2. wow really nice. It will be helpful for the people those who are ready to crack the interview and please also for remind what they have learned throughout concept.

    Back to Original

    ReplyDelete

Select Menu