Limit the Class Object Creation

In this section you will learn about how a class object creation can be a restricted to a fix number of times.


 

Ads

Tutorials   
What are the benefits of learning Java Programming? A brief history and future of Java programming language Importance of Java for Mobile Application Development How To Become A Good Software Developer? Learn Java Programming for beginners 5 Reasons Java is Still a Great Language to Boost Your Career Prospect as a Programmer How to learn Java with no programming experience? How to learn Java? What are Classes and Interfaces in Java? Converting a String to Integer in Java How to check if a string exists in a string in JavaScript? Installing JDK 8 on Windows 10 and configuring the settings Video Tutorial of reading file in Java 9 Invincible Reasons to learn Java New to Java programming Java Programming Tips, Articles and Notes How to download JDK 1.6? Encapsulation in Java Programming How to format number in Java? Generics Example Program in Java Java 9 Features Java 9 Tutorial, news and examples How to read content of JPEG file in Java? Image Processing Tutorials in Java Programming Language Introduction to Java Technology Java 8 Consumer Interface with forEach Loop Introduction to Generics in Java Java 8 consumer class(interface) example Oracle Launches Project Valhalla for Java How to read properties file in Java? How to add JDK 8 support in Eclipse? Java 8: Java 8 is officially released and it can be downloaded How to Invoke method using Reflection API of Java? First Lambda Expressions in Java Java 8 expected release date Lambda Expressions example in Eclipse JDK 8 Release Candidates Available Lambda Calculus Lambda Expression in Java 8 How to learn Java easily? Java made easy Oracle to release Java 8 in March 2014 Object Oriented Programming in Java Java Training Free Java beginners Course Easiest way to learn Java Tutorials on Java JDK 8 - Developer Preview Released Step by Step Java tutorials for beginners Learn the Java 8 and master the new features of JDK 8
Ads

Limit the Class Object Creation

In this section you will learn about how a class object creation can be a restricted to a fix number of times.

In this tutorial I am giving a sample code which will demonstrate you about how to restrict the object creation of a class. This is a simple code in Java where I have created a class named ClassInstance which contains the two static data members named ci and counter. The data member ci is of ClassInstance type and the other is the counter used to count the number of times the object created. Besides these this class contained getClassInstance() and main() methods. The getClassInstance() method returns the class object. and in the main method I have created the objects of this class and added them into a list and traversed this list.

Sample Codes

ClassInstance.java

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public final class ClassInstance {

private static ClassInstance ci;
private static int counter=0;

private ClassInstance() {
counter++; 
}
public static ClassInstance getClassInstance() {
if(counter<5)
{
ci = new ClassInstance();
}
else if(counter >=5)
{
ci = new ClassInstance();
ci=null; 
} 
return ci;
}

public static void main(String[] args)
{
try{

List<ClassInstance> list = new ArrayList<ClassInstance>();
ClassInstance obj1 = ClassInstance.getClassInstance();
ClassInstance obj2 = ClassInstance.getClassInstance();
ClassInstance obj3 = ClassInstance.getClassInstance();
ClassInstance obj4 = ClassInstance.getClassInstance();
ClassInstance obj5 = ClassInstance.getClassInstance();
ClassInstance obj6 = ClassInstance.getClassInstance();
ClassInstance obj7 = ClassInstance.getClassInstance();
ClassInstance obj8 = ClassInstance.getClassInstance();
list.add(obj1);
list.add(obj2);
list.add(obj3);
list.add(obj4);
list.add(obj5);
list.add(obj6);
list.add(obj7);
list.add(obj8);
ListIterator<ClassInstance> itr = list.listIterator();
while(itr.hasNext())
{ 
System.out.println(itr.next().hashCode()); 
}
}
catch(Exception e)
{
try
{
if(counter >= 5)
{
MyException.counter = counter;
}
throw new MyException();
}
catch(MyException e1)
{
System.out.println("Exception@ "+ClassInstance.class+" : "+e1);
}
}
}
}

MyException.java

public class MyException extends Exception{

static String msg = "";
static int counter ;
public MyException(){
super(msg);
}
public MyException(String str){
super(str);
}
public String toString()
{
if (counter >= 5)
msg = "Object creation exceeded the limit Only 5 times object creation is allowed";
return msg;
}
}

Output

I have made this example using Eclipse IDE so the output is in its perspective.

Download Source Code

Advertisements

Ads
Share on Google+Share on Google+

Limit the Class Object Creation

Posted on: October 17, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Limit the Class Object Creation  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads