In class B, how can I create an object of class A other than the process of object creation (i.e. without creating an object having null)?
class A
{
public int one;
A(A a)
{
a.one=1;
}
}
class B
{
public static void main(String...args)
{
//now how to create an object of class A over here.
}
}
You cannot normally do this in Java.
However, it’s possible with heavy cheating. Don’t do this in production code. But for the sake of argument:
1) With Mockito (But won’t work with a security manager):
2) Here’s another way to do it overriding
finalize().