3 ways to initialize an object in Java

Java Programming Java8Object Oriented Programming

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();
raja
Published on 23-Apr-2018 10:34:51
Advertisements