Java / Constructor
Different ways to create an object in Java.
There are 5 different ways to create an object in Java.
Using new keyword.
MyObject object = new MyObject();
Using Class.forName().
Quiz object = (Quiz) Class.forName("net.javapedia.quiz.model.Quiz").newInstance();
Using clone().
Quiz object1 = new Quiz(); Quiz object2 = (Quiz ) object1.clone();
Using object deserialization.
ObjectInputStream inStream = new ObjectInputStream(myInputStream ); Quizobject = (Quiz) inStream.readObject();
Using Reflection newInstance() method of Constructor.
Constructor<Quiz> constructor = Quiz.class.getConstructor(); Quiz quiz = constructor.newInstance();
More Related questions...