
hi all, can i restrict a class that it will not allows create more than two objects can any help me please.
Thaks bala k

You can restrict a class to make it's "n" number of object , please follow the below mentiond code.
public final class NnumberInstance {
private static NnumberInstance obj[];
private NnumberInstance() {
}
public static NnumberInstance[] getInstance(int n) {
obj = new NnumberInstance[n];
for (int i = 0; i < n; i++) {
obj[i] = new NnumberInstance();
}
return obj;
}
}
public static NnumberInstance obj[];
public static void main(String[] args) {
obj = NnumberInstance.getInstance(5);
for (NnumberInstance instance : obj) {
System.out.println("------------------------"+instance.hashCode());
}
}
}

public final class NnumberInstance {
private static NnumberInstance obj[];
private NnumberInstance() {
}
public static NnumberInstance[] getInstance(int n) {
obj = new NnumberInstance[n];
for (int i = 0; i < n; i++) {
obj[i] = new NnumberInstance();
}
return obj;
}
}
public class MainJavaClass {
public static NnumberInstance obj[];
public static void main(String[] args) {
obj = NnumberInstance.getInstance(5);
for (NnumberInstance instance : obj) {
System.out.println("------------------------"+instance.hashCode());
}
}
}