Is there a way where we can restrict a class to create only a single object in java? It should give some exceptions if we try to create another new object. Example:
class A {}
public class Test {
public static void main(String[] args) {
A a1 =new A(); //This should be allowed
A a2 =new A(); // This should not be allowed
}
}