Ans:
1. Using new keyword
This is the most common way to create an object in java.In This we will be using the new operator which will allocates the memory space and initialize the field with default value. Then it executes the code inside the specified constructor which normally re-writes the default values with the value inside the constructor.
1 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.
1 MyObject object (MyObject) Class.forName( vinay.abc.MyObject ).newInstance();
vinay.abc.MyObject – is the class name
3. Using clone()
The clone() can be used to create a copy of an existing object. Object.clone() is a native method which translates into instructions for allocating the memory and copying the data.Even if you override the clone method then also you need to call the super.clone() method inside the overridden clone method. In this method a copy instruction is called and which copy the data from original object to clone object.
1. MyObject anotherObject new MyObject();
2. MyObject object anotherObject.clone();
Object deserialization is nothing but creating an object from its serialized form. It will uses the ‘new’ operator internally and always calls the default constructor.
1 ObjectInputStream inStream new ObjectInputStream(anInputStream );
2 MyObject object (MyObject) inStream.readObject();
5. Using class loader
one more is through creation of object using classloader like
1 this.getClass().getClassLoader().loadClass( com.amar.myobject ).newInstance
4 comments:
Hey buddy, 2nd way i.e. Class.forName() must be as follows;
MyObject object = (MyObject) Class.forName("vinay.abc.MyObject").newInstance();
// here, vinay.abc.MyObject has to be in double quotes
// otherwise complile time error will be thrown
Hey buddy, 5th way i.e. class loader must be as follows;
package com.linkedin;
public class MyObject
{
public MyObject getObj()
{
MyObject object1 = null;
try {
object1 = (MyObject) this.getClass().getClassLoader().loadClass("com.linkedin.MyObject").newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object1;
}
public static void main(String[] args)
{
try {
MyObject object = (MyObject) Class.forName("com.linkedin.MyObject").newInstance();
System.out.println("Gotcha !!!! "+object);
MyObject object3 = new MyObject();
MyObject object5 = (MyObject)object3.getObj();
System.out.println("Gotcha !!!! "+object5);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// I mean we can't create object using class loader in // static conext i.e. main() or any static method
On a high level basis, we can say that object creation happens in three ways:
1. instance initialization
2. intsance variables initialization
3. constructors
they are multiple ways to create object in java,
1.by using "new" keyword
ex: A a=new A();
2.by using "newInstance()" method
Class.forName("A").newInstance();
3. factory method:
one class method gives another class object is nothing but factory mathod.
ex 1: Class c=Class.forName("A");
String ss=c.getName();
here "ss" object created on factory method.
4.instance factory method:
one method is gives same that class object by calling with object that is instance factory.
ex: String name="swati";
String lastname="java";
String fullname=name.concat(lastname);
here "fullname" also object creating by using instatnce factory method.
5.static factory method
one class static method gives same class object then it is static factory. it is very usefull when constructor having "private" modifier
ex:1 Thread th=Thread.currentThread();
EX:2 Class c=Class.forname("classname");
ex: 3 Runtime r=Runtime.getInstance();
6.cloning process.
A a=new A();
A b=(A)a.clone();
7.deserialization process.
Post a Comment