You must have used the “new” operator to create an Object of a Class. But is it the only way to create an Object?
Simple Answers is NO, then in how many ways we can create Object of a Class. There are several like
- Using New keyword
- Using New Instance (Reflection)
- Using Clone
- Using Deserilization
- Using ClassLoader
- … don’t know
Now we will explore the different ways of creating the Object except new Operator.
Using New Keyword
Using new keyword is the most basic way to create an object. Use new keyword to create and Object of class.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class ObjectCreationExample {
public static void main(String[] args) {
// Here we are creating Object of JBT using new keyword
JBT obj = new JBT();
}
}
class JBT{
String Owner;
}
|
Using New Instance (Reflection)
Have you ever tried to connect to any DB using JDBC driver in Java, If your answer is yes then you must have seen “Class.forName“. We can also use it to create the object of a class. Class.forName actually loads the class in Java but doesn’t create any Object. To Create an Object of the Class you have to use newInstance method of Class class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/*
* Here we will learn to create Object of a class without using new Operator.
* But newInstance method of Class class.
*/
class CreateObject {
public static void main(String[] args) {
try {
Class cls = Class.forName("JBTClass");
JBTClass obj = (JBTClass) cls.newInstance();
JBTClass obj1 = (JBTClass) cls.newInstance();
System.out.println(obj);
System.out.println(obj1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
class JBTClass {
static int j = 10;
JBTClass() {
i = j++;
}
int i;
@Override
public String toString() {
return "Value of i :" + i;
}
}
|
Note*: If you want to create Object in this way class needs to have public default Constructor.
Using Clone
We can also use Clone() method to create a copy of an existing Object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/*
* Here we will learn to create an Object of a class without using new Operator.
* For this purpose we will use Clone Interface
*/
class CreateObjectWithClone {
public static void main(String[] args) {
JBTClassClone obj1 = new JBTClassClone();
System.out.println(obj1);
try {
JBTClassClone obj2 = (JBTClassClone) obj1.clone();
System.out.println(obj2);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
class JBTClassClone implements Cloneable {
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
int i;
static int j = 10;
JBTClassClone() {
i = j++;
}
@Override
public String toString() {
return "Value of i :" + i;
}
}
|
Note*:
- Here we are creating the clone of an existing Object and not any new Object.
- Clone method is declared protected in Object class. So it can be accessed only in subclass or in same package. That is the reason why it has been overridden here in Class.
- Class need to implement Cloneable Interface otherwise it will throw CloneNotSupportedException.
Using Object Deserialization
Object deserialization can also be used to create an Object. It is just the opposite of serializing an Object.
Using ClassLoader
We can also use Class Loader to create Object of a Class. This way is some what same as Class.forName option.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
/*
* Here we will learn to Create an Object using Class Loader
*/
public class CreateObjectWithClassLoader {
public static void main(String[] args) {
JBTClassLoader obj = null;
try {
obj = (JBTClassLoader) new CreateObjectWithClassLoader().getClass()
.getClassLoader().loadClass("JBTClassLoader").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println(obj);
}
}
class JBTClassLoader {
static int j = 10;
JBTClassLoader() {
i = j++;
}
int i;
@Override
public String toString() {
return "Value of i :" + i;
}
}
|