who can we create the object of a class? in how many ways we
can create it (max 5)

Answer Posted / chandni jain

1. Using new keyword
This is the most common way to create an object in java.

MyObject object = new MyObject();

2. Using Class.forName()
If we know the name of the class & if it has a public
default constructor we can create an object in this way.

MyObject object = (MyObject)
Class.forName("subin.rnd.MyObject").newInstance();

3. Using clone()
The clone() can be used to create a copy of an existing object.

MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();

4. Using object deserialization
Object deserialization is nothing but creating an object
from its serialized form.

ObjectInputStream inStream = new
ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is java good for beginners?

769


What does += mean in java?

806


What is java’s garbage collected heap?

740


Does substring start with 0?

768


Hi Anyone know the model / questions of the Federal bank sample questions for the post of Specialist Officers - Programmers. Please post if anyone have..

2077


What is the difference between compiler and jvm?

767


What is numeric function?

745


What are pass by reference and pass by value?

725


What is bubble sort in java?

859


What are wrapped classes in java programming?

836


Can a hashset contain duplicates java?

717


Explain about data types?

791


What do you mean by boolean?

835


What is indexof?

740


What is file in java?

798