How is object created in java?

Answers were Sorted based on User's Feedback



How is object created in java?..

Answer / ishaque

There are many ways to create an object in java. They are:

By new keyword
By newInstance() method
By clone() method
By factory method etc.

Is This Answer Correct ?    15 Yes 1 No

How is object created in java?..

Answer / akshay

Object is created following ways
1.Using NEW operator
2.Class.newinstance()

Is This Answer Correct ?    7 Yes 1 No

How is object created in java?..

Answer / javamasque

Object is created in four different ways
1. Using new operator
2. Class.forName(..)
3. Cloning
4. Serializing and De-serializing

Is This Answer Correct ?    6 Yes 2 No

How is object created in java?..

Answer / chantiraji

There are FIVE different ways to create objects in Java:

1. Using `new` keyword:

This is the most common way to create an object in Java. Almost 99% of objects are created in this way.

MyObject object = new MyObject();//normal way
2. By Using Factory Method:

ClassName ObgRef=ClassName.FactoryMethod();
Example:

RunTime rt=Runtime.getRunTime();//Static Factory Method
3. By Using Cloning Concept:

By using clone(), the clone() can be used to create a copy of an existing object.

MyObjectName anotherObject = new MyObjectName();
MyObjectName object = anotherObjectName.clone();//cloning Object
4. 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.

MyObjectName object = (MyObjectNmae) Class.forName("PackageName.ClassName").newInstance();
Example:

String st=(String)Class.forName("java.lang.String").newInstance();
5. Using object deserialization:

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

ObjectInputStreamName inStream = new ObjectInputStreamName(anInputStream );
MyObjectName object = (MyObjectNmae) inStream.readObject();

Is This Answer Correct ?    2 Yes 0 No

How is object created in java?..

Answer / anu sownthariya rajakumari,isw

1.new
2.newinstance()
3.clone()
4.class.forName()
5.deserialization

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; String s1 = s1+?XYZ?; s.o.p(s1); The output is ABCXYZ, which is the value of s1 ?

6 Answers   Flextronics, Keane India Ltd,


What is the purpose of using javap?

0 Answers  


what should do when using multiple catch() block & what should never do for the same?

1 Answers  


Can we declare an interface as final?

0 Answers  


Can we define private and protected modifiers for the members in interfaces?

0 Answers  






What is * argv?

0 Answers  


How is Object Oriented Programming different from Procedure Oriented Programming?

0 Answers   Amdocs,


What is a numeric digit?

0 Answers  


What is ternary operator?

0 Answers  


Where is core java used?

0 Answers  


Why string is immutable with example?

0 Answers  


Which Math method is used to calculate the absolute value of a number?

3 Answers   Accenture,


Categories