Here is example how we can restrict someone to create a limited objects of a class :-
class Example{
private static int count;
private static int MAX_COUNT = 5;
private Example(){
}
public static Example getObject(){
if(count < MAX_COUNT){
count ++;
return new Example();
}
else{
System.out.println("You exceded the limit");
//TO DO
}
}
private static int count;
private static int MAX_COUNT = 5;
private Example(){
}
public static Example getObject(){
if(count < MAX_COUNT){
count ++;
return new Example();
}
else{
System.out.println("You exceded the limit");
//TO DO
}
}
0 comments:
Post a Comment