Question: How to restrict a class from creating multiple objects?
Basic java example program to restrict a class from not to create more than three instances
Output:
You Might Like:
1.Five Different places to create object of a class
2.Five different ways to create objects in java?
3.Six different ways to iterate list in java
- 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
- package com.instanceofjava;
- public class RestrictObjectCreation{
- private static RestrictObjectCreationobject;
- public static int objCount = 0;
- private RestrictObjectCreation()
- {
- System.out.println("Singleton(): Private constructor invoked");
- objCount ++;
- }
- public static RestrictObjectCreation getInstance()
- {
- if (objCount < 3)
- {
- object = new RestrictObjectCreation();
- }
- return object;
- }
- }
- package instanceofjava;
- public class Test{
- public static void main(String args[]) {
- RestrictObjectCreation obj1= RestrictObjectCreation.getInstance();
- RestrictObjectCreation obj2= RestrictObjectCreation.getInstance();
- RestrictObjectCreation obj3= RestrictObjectCreation.getInstance();
- RestrictObjectCreation obj4= RestrictObjectCreation.getInstance();
- RestrictObjectCreation obj5= RestrictObjectCreation.getInstance();
- System.out.println(obj1.hashCode());
- System.out.println(obj2.hashCode());
- System.out.println(obj3.hashCode());
- System.out.println(obj4.hashCode());
- System.out.println(obj5.hashCode());
- }
- }
Output:
- RestrictObjectCreation(): Private constructor invoked
- RestrictObjectCreation(): Private constructor invoked
- RestrictObjectCreation(): Private constructor invoked
- 705927765
- 366712642
- 1829164700
- 1829164700
- 1829164700
You Might Like:
1.Five Different places to create object of a class
2.Five different ways to create objects in java?
3.Six different ways to iterate list in java
very good explanation sir.move on
ReplyDeletewow 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.
ReplyDeleteBack to Original