You must have used the “new” operator to create an Object of a Class. But is it the only way to create an Object?
Simple Answers is NO, then in how many ways we can create Object of a Class. There are several like
Simple Answers is NO, then in how many ways we can create Object of a Class. There are several like
- Using New Instance
- Using Clone
- Using Deserilization
- Using ClassLoader
- … don’t know
Live Exmaple :
Just Copy Code and run the example
ChildClass.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package objects;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* %WY
*
* @version 1.1
* @since practice 8/28/13
*
* @author Srikanth Ganji
*
*/
public class ChildClass {
private ParrentClass dummyObject=null;
private Class class1=null;
private ParrentClass firstType()
{
System.out.println("\n\n>>>>>>>>>>>>> 1) Normal Way Creating Object <<<<<<<<<<<<<<<<< ");
System.out.println("\n \t using new Operator. \n\t Eg:- new ParrentClass(); ");
return new ParrentClass();
}
private ParrentClass secondType() throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
try {
System.out.println("\n\n>>>>>>>>>> 2) Created Object Using Class.forName(\"\"); <<<<<<<<<<<<<<<<<");
System.out.println("\n Class.forName(\"objects.ParrentClass\"); ");
System.out.println("\n \tInner Opperation \n\t We can also use it to"
+ "\ncreate the object of a class. Class.forName actually loads the"
+ "\n class in Java but doesn’t create any Object. To Create an Object"
+ "\n of the Class you have to use newInstance method of Class class. ");
class1=Class.forName("objects.ParrentClass");
dummyObject=(ParrentClass)class1.newInstance();
} catch (ClassNotFoundException ex) {
throw new ClassNotFoundException("Excepiton on Load Class in Java ");
} catch (InstantiationException ex) {
throw new InstantiationException("Exception on Instiallizing Object Class Object Initialted to Object");
} catch (IllegalAccessException ex) {
throw new IllegalAccessException("Excetion on IllegalAccessExcepion On Class Object Initialted to Object");
}
return dummyObject;
}
private ParrentClass thiredType() throws CloneNotSupportedException
{
System.out.println("\n\n>>>>>>>>>>>> 3) Creating Object Using Clone <<<<<<<<<<<<<<<<<");
System.out.println("\n\t We can also use Clone() method to create a copy of an existing Object. ");
try {
dummyObject=ParrentClass.INSTANCE.clone();
System.out.println("\n \t Ex:- dummyObject=ParrentClass.INSTANCE.clone(); ");
System.out.println("\" Note \" \n \t 1) Here we are creating the clone of an existing Object and not any new Object. ");
System.out.println(" \n\t 2) Clone method is declared protected in Object class. So it can be accessed only"
+ " \n\t in subclass or in same package. That is the reason why it has been overridden here in Class. ");
System.out.println("\n\t 3)Class need to implement Cloneable Interface otherwise \n\t it will throw CloneNotSupportedException. ");
} catch (CloneNotSupportedException ex) {
throw new CloneNotSupportedException("\n Exception Raised when Object Cloning ");
}
return dummyObject;
}
private ParrentClass fourthType()
{
System.out.println("\n\n>>>>>>>>>>>>> 4) Creating Object Using Factory Methods ");
System.out.println("\n\t public static ParrentClass INSTANCE=getInstance(); ");
System.out.println("\n\t public static ParrentClass getInstance() \n"
+" { "
+ "\n return new ParrentClass();"
+ "\n } ");
System.out.println("\n\t return ParrentClass.INSTANCE; ");
return ParrentClass.INSTANCE;
}
private ParrentClass fifthType() throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
System.out.println("\n\n 5)>>>>>>>>>>>>>> Creating Object Like CLass.forName(\"\"); <<<<<<<<<<<<");
try {
System.out.println("\n\t Ex:- \n\t"
+ " dummyObject=(ParrentClass) this.getClass() \n" +
" .getClassLoader().loadClass(\"objects.ParrentClass\").newInstance(); ");
dummyObject=(ParrentClass) this.getClass()
.getClassLoader().loadClass("objects.ParrentClass").newInstance();
} catch (ClassNotFoundException ex) {
throw new ClassNotFoundException("Excepiton on Load Class in Java ");
} catch (InstantiationException ex) {
throw new InstantiationException("Exception on Instiallizing Object Class Object Initialted to Object");
} catch (IllegalAccessException ex) {
throw new IllegalAccessException("Excetion on IllegalAccessExcepion On Class Object Initialted to Object");
}
return dummyObject;
}
public static void main(String...str)
{
ChildClass childClass=new ChildClass();
ParrentClass obj1,obj2,obj3,obj4,obj5;
obj1=childClass.firstType();
try {
obj2=childClass.secondType();
} catch (ClassNotFoundException ex) {
System.out.println(" "+ex.getMessage());
ex.printStackTrace();
} catch (InstantiationException ex) {
System.out.println(" "+ex.getMessage());
ex.printStackTrace();
} catch (IllegalAccessException ex) {
System.out.println(" "+ex.getMessage());
ex.printStackTrace();
}
try {
obj3=childClass.thiredType();
} catch (CloneNotSupportedException ex) {
System.out.println(" "+ex.getMessage());
ex.printStackTrace();
}
obj4=childClass.fourthType();
try {
obj5=childClass.fifthType();
} catch (ClassNotFoundException ex) {
System.out.println(" "+ex.getMessage());
ex.printStackTrace();
} catch (InstantiationException ex) {
System.out.println(" "+ex.getMessage());
ex.printStackTrace();
} catch (IllegalAccessException ex) {
System.out.println(" "+ex.getMessage());
ex.printStackTrace();
}
}
}
ParrentClass.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package objects;
/**
*
* @author Srikanth Ganji
*/
public class ParrentClass implements Cloneable{
int x=100;
@Override
protected ParrentClass clone() throws CloneNotSupportedException {
return (ParrentClass) super.clone();
}
public static ParrentClass INSTANCE=getInstance();
public static ParrentClass getInstance()
{
return new ParrentClass();
}
public ParrentClass getObject()
{
return this;
}
}
No comments:
Post a Comment