The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Example program to restrict a class from creating not more than three objects

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java Example program to restrict a class from creating not more than three objects Posted: Mar 14, 2016 2:22 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java Example program to restrict a class from creating not more than three objects
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
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

Read: Java Example program to restrict a class from creating not more than three objects

Topic: How to connect Eclipse to Oracle database - Step by Step Guide Previous Topic   Next Topic Topic: How to convert java.util.Date to java.sql.Timestamp in Java - JDBC Example

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use