JAVA CODE QUESTION

HOW TO CREATE AN OBJECT OF THE CLASS WITHOUT USING NEW OPERATOR

THE ANSWER IS GIVEN IN THE COMMENTS

1 comments:

gautam said...

HERE IS THE ANSWER

class A {
int i=10;
}
public class Demo1 {
public static void main(String str[]) {
try {
Class c=Class.forName("A");//Returns the Class object associated with the class with the given string name
Object o=c.newInstance();//Creates a new instance of the class represented by this Class object.
A a=(A) o;//casting

System.out.println(a.i);
}
catch(Exception e){
System.out.println("Exception"+e);
}
}
}

Post a Comment