In this article, we will discuss how to prevent and throw exception while creating 2nd instance/object of a Class
1. Problem statement :
- Given Class A, Class B extends A and Class C extends B
- If we create A a1 = new A() for the very first time then one instance/object of Class A should be created but when we try create another instance of Class A for the 2nd time then program should throw Exception
- Similarly, the next Class B which extends A should throw Exception, when 2nd time we try to instantiate Class B
- Likewise, for third Class C which extends B should throw Exception when we instantiate Class C for the 2nd time
2. Solution/Implementation :
- We defined top-most Class A
- Then we have defined Class B which extends A and another Class C which extends B
- Inside constructor of Class A, we are trying to add corresponding Class (A, B, C) while instantiating using add() method of HashSet
- If add() method returns false which means there is already one object is instantiated and stored of same-type and in this case we will throw Exception for that particular Class
- As Class B extends A and Class C extends B it will also exhibit same behavior like that of Class A via super-constructor call ( or constructor-chaining)
- Note: Class B & C has constructor with simple sysout to understand the flow/behavior of the program and super() constructor call is optional
A.java ( B.java and C.java )
package interview.program;
import java.util.HashSet;
import java.util.Set;
// Inheritance : top-most class A
public class A {
// create HashSet instance to store object
private static Set<String> createdInstanceList = new HashSet<String>();
// default constructor for A()
A() throws Exception {
// while adding same class, return false
if(!createdInstanceList.add(this.getClass().toString())) {
// throw exception for 2nd time object creation
throw new Exception("Trying to create 2nd time - "
+ this.getClass().toString());
}
else {
// this will print only for the 1st time object creation
System.out.println("A() constructor invoked ...\n");
}
}
}
// Inheritance : 2nd level class B which extends A
class B extends A {
// default constructor for B()
B() throws Exception {
super(); // super call, optional
System.out.println("B() constructor invoked ...\n");
}
}
// Inheritance : 3rd level class C which extends B
class C extends B {
// default constructor for C()
C() throws Exception {
super(); // super call, optional
System.out.println("C() constructor invoked ...\n");
}
}
3. Testing different scenarios :
3.1 Testing scenario 1 – success
- We are trying to create instance of Classes A, B and C for the very first time (just once)
TestingMain1.java
package interview.program;
public class TestingMain1 {
public static void main(String[] args) {
try {
System.out.println("Trying to instantiate 1st time A :- \n");
A a = new A();
System.out.println("Trying to instantiate 1st time B :- \n");
B b = new B();
System.out.println("Trying to instantiate 1st time C :- \n");
C c = new C();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
- As we are creating instance of Classes A, B and C once, it will create successfully as shown in the console output below
Trying to instantiate 1st time A :-
A() constructor invoked ...
Trying to instantiate 1st time B :-
A() constructor invoked ...
B() constructor invoked ...
Trying to instantiate 1st time C :-
A() constructor invoked ...
B() constructor invoked ...
C() constructor invoked ...
3.2 Testing scenario 2 – trying to instantiate Class A twice
- We are trying to create instance of Class A twice
TestingMain2.java
package interview.program;
public class TestingMain2 {
public static void main(String[] args) {
try {
System.out.println("Trying to instantiate 1st time A :- \n");
A a = new A();
System.out.println("Trying to instantiate 2nd time A :- \n");
A a2 = new A();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
- As we are trying to create instance of Class A twice therefore an Exception is thrown as shown in the console output below
Trying to instantiate 1st time A :-
A() constructor invoked ...
Trying to instantiate 2nd time A :-
java.lang.Exception: Trying to create 2nd time - class interview.program.A
at interview.program.A.<init>(A.java:19)
at interview.program.TestingMain2.main(TestingMain2.java:13)
3.3 Testing scenario 3 – trying to instantiate Class B twice
- We are trying to create instance of Class B twice
TestingMain3.java
package interview.program;
public class TestingMain3 {
public static void main(String[] args) {
try {
System.out.println("Trying to instantiate 1st time B :- \n");
B b = new B();
System.out.println("Trying to instantiate 2nd time B :- \n");
B b2 = new B();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
- As we are trying to create instance of Class B twice therefore an Exception is thrown as shown in the console output below
Trying to instantiate 1st time B :-
A() constructor invoked ...
B() constructor invoked ...
Trying to instantiate 2nd time B :-
java.lang.Exception: Trying to create 2nd time - class interview.program.B
at interview.program.A.<init>(A.java:19)
at interview.program.B.<init>(A.java:36)
at interview.program.TestingMain3.main(TestingMain3.java:13)
3.4 Testing scenario 4 – trying to instantiate Class C twice
- We are trying to create instance of Class C twice
TestingMain4.java
package interview.program;
public class TestingMain4 {
public static void main(String[] args) {
try {
System.out.println("Trying to instantiate 1st time C :- \n");
C c = new C();
System.out.println("Trying to instantiate 2nd time C :- \n");
C c2 = new C();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
- As we are trying to create instance of Class C twice therefore an Exception is thrown as shown in the console output below
Trying to instantiate 1st time C :-
A() constructor invoked ...
B() constructor invoked ...
C() constructor invoked ...
Trying to instantiate 2nd time C :-
java.lang.Exception: Trying to create 2nd time - class interview.program.C
at interview.program.A.<init>(A.java:19)
at interview.program.B.<init>(A.java:36)
at interview.program.C.<init>(A.java:46)
at interview.program.TestingMain4.main(TestingMain4.java:13)
3.5 Testing scenario 5 – trying to instantiate Class A 2nd time
- We are trying to create instance of Class A for 2nd time after instantiating Classes A, B and C once already
TestingMain5.java
package interview.program;
public class TestingMain5 {
public static void main(String[] args) {
try {
System.out.println("Trying to instantiate 1st time A :- \n");
A a = new A();
System.out.println("Trying to instantiate 1st time B :- \n");
B b = new B();
System.out.println("Trying to instantiate 1st time C :- \n");
C c = new C();
System.out.println("Trying to instantiate 2nd time A :- \n");
A a2 = new A();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
- As we are trying to create instance of Class A twice therefore an Exception is thrown after instantiating Classes A, B and C once successfully, as shown in the console output below
Trying to instantiate 1st time A :-
A() constructor invoked ...
Trying to instantiate 1st time B :-
A() constructor invoked ...
B() constructor invoked ...
Trying to instantiate 1st time C :-
A() constructor invoked ...
B() constructor invoked ...
C() constructor invoked ...
Trying to instantiate 2nd time A :-
java.lang.Exception: Trying to create 2nd time - class interview.program.A
at interview.program.A.<init>(A.java:19)
at interview.program.TestingMain5.main(TestingMain5.java:19)
3.6 Testing scenario 6 – trying to instantiate Class B 2nd time
- We are trying to create instance of Class B for 2nd time after instantiating Classes A, B and C once already
TestingMain6.java
package interview.program;
public class TestingMain6 {
public static void main(String[] args) {
try {
System.out.println("Trying to instantiate 1st time A :- \n");
A a = new A();
System.out.println("Trying to instantiate 1st time B :- \n");
B b = new B();
System.out.println("Trying to instantiate 1st time C :- \n");
C c = new C();
System.out.println("Trying to instantiate 2nd time B :- \n");
B b2 = new B();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
- As we are trying to create instance of Class B twice therefore an Exception is thrown after instantiating Classes A, B and C once successfully, as shown in the console output below
Trying to instantiate 1st time A :-
A() constructor invoked ...
Trying to instantiate 1st time B :-
A() constructor invoked ...
B() constructor invoked ...
Trying to instantiate 1st time C :-
A() constructor invoked ...
B() constructor invoked ...
C() constructor invoked ...
Trying to instantiate 2nd time B :-
java.lang.Exception: Trying to create 2nd time - class interview.program.B
at interview.program.A.<init>(A.java:19)
at interview.program.B.<init>(A.java:36)
at interview.program.TestingMain6.main(TestingMain6.java:19)
3.7 Testing scenario 7 – trying to instantiate Class C 2nd time
- We are trying to create instance of Class C for 2nd time after instantiating Classes A, B and C once already
TestingMain7.java
package interview.program;
public class TestingMain7 {
public static void main(String[] args) {
try {
System.out.println("Trying to instantiate 1st time A :- \n");
A a = new A();
System.out.println("Trying to instantiate 1st time B :- \n");
B b = new B();
System.out.println("Trying to instantiate 1st time C :- \n");
C c = new C();
System.out.println("Trying to instantiate 2nd time C :- \n");
C c2 = new C();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
- As we are trying to create instance of Class C twice therefore an Exception is thrown after instantiating Classes A, B and C once successfully, as shown in the console output below
Trying to instantiate 1st time A :-
A() constructor invoked ...
Trying to instantiate 1st time B :-
A() constructor invoked ...
B() constructor invoked ...
Trying to instantiate 1st time C :-
A() constructor invoked ...
B() constructor invoked ...
C() constructor invoked ...
Trying to instantiate 2nd time C :-
java.lang.Exception: Trying to create 2nd time - class interview.program.C
at interview.program.A.<init>(A.java:19)
at interview.program.B.<init>(A.java:36)
at interview.program.C.<init>(A.java:46)
at interview.program.TestingMain7.main(TestingMain7.java:19)
Post credits : Shivam Agarwal and Sanket Chalke
Happy Coding !!
Happy Learning !!