3 ways to initialize an object in Java



Consider a class Tester which has implemented Cloneable interface. Now you can initialize an object using following three ways −

1. Using new keyword.

Tester tester1 = new Tester();

2. Using Class.forName() method

Tester tester4 = (Tester)Class.forName("Tester").newInstance();

3. Using clone method.

Tester tester2 = tester1.clone();

Advertisements