D
D
Denis Mashanov2016-12-08 08:30:57
Java
Denis Mashanov, 2016-12-08 08:30:57

How, having launched once class1, to use the created values ​​in other classes without using the constructor?

I can't understand one thing with classes. Let's say I have class1, class2, class3, class4. We launched class1, in this class we initialized class2. How now, when calling class3, and then class4, get the already initialized class2 objects in class1 without using constructors?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2016-12-08
@LoneRay

What is wrong with passing class1, class2 objects through the constructor to class3, class4 ?
You can bring in class3, for example, a field with class1 type, and make a setter for this field. Then, after initializing class3, you will need to put an instance of class1 into it through the setter.
For more "nice" ways people use Dependency Injection. Spring, for example, has a good DI mechanism. And if you look into its giblets, then you will see that it does exactly the options I described: dependency injection through a constructor, or through a setter. No, there is of course Reflection, but you probably are not interested in it yet.
All of the above is true for Java development.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question