How can I create an object of this class?

In class B how can I create an object of class A different from the process of creating an object (i.e. without creating an object with 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. } } 
+6
source share
6 answers

Usually you cannot do this in Java.

However, this is possible with heavy deception. Do not do this in production code. But for the sake of argument:

1) With Mockito (but will not work with the security manager):

 import org.mockito.Mockito; class B { public static void main(String... args) { A mockedA = Mockito.mock(A.class); A realA = new A(mockedA); System.out.println(realA); } } 

2) Here is another way to do this by overriding finalize() .

+3
source

To build an object of type A, you need to pass a constructor a link to another object of type A or a null reference. Thus, you have only two options:

 A a1 = new A(null); A a2 = new A(a1); 

The first time you create an object of type A , you must use a null reference because you do not have other objects of type A


Update

After you change your question, I don’t think you can build an object of type A

+7
source

If I understand your question correctly, you want to create an object a without transferring to it a pre-existing object of the same type.

To do this, you need to add a constructor to class A, which does not accept a parameter of type A or does not modify an existing one:

 class A { A() { // Constructor logic. } A(A a) { // Constructor logic when passing an existing object of the same type, perhaps to create a clone. } } 

If for some reason you cannot change class A, you will need to follow Mark Bayer's answer and pass a null reference to the constructor.


Update

With updating your code, this problem (or a thought experiment) is insoluble: class A cannot be created as written.

+5
source

The only way to create an object is to provide it with another object A in the constructor, but for this other object A, you also need another object A in the constructor. This will stop only when setting the null reference in the constructor.

Edit

My answer was based on your previous version of the code. Now you cannot create an object because you cannot put null in the constructor (an empty field does not have one ). If you want to create an object A, you need to check for null in the constructor, for example if (a!=null){ a.one=1;} , and pass null or an already created object in the constructor.

+3
source

If A is sealed (cannot be edited), you can extend this class and give it a default constructor to pass it to a new instance of A. There is no other solution without changing the source code of A.

+2
source

You cannot create an instance of A in your example. This is a problem with chicken and egg. You need instance A to create instance A, but you cannot create instance A without instance A.

So, without changes to (whether adding a β€œnull” check or adding a default constructor) you cannot create an instance of A in this scenario.

+2
source

Source: https://habr.com/ru/post/917237/


All Articles