0

I'm currently working on a Java project. I'm quite new to Java. I have done C++/C# in the past.

I have a question regarding dependency injection. The basic idea why to use it is already clear to me.

public class MyClass
{
  @Inject
  private IFoo fooInterface;

  @Inject
  private Bar barClass;
  
}

with

public interface iFoo
{
}

public class Bar
{
}

Now it makes perfectly sense to inject an actual interface implementation to reduce dependencies.

But why would I like to inject a "normal" class with does not implement an interface?

Could you help me understand?

Edit: Thank you guys for all your comments. I believe I'm getting now the idea behind dependency injection.

Just to clarify: After speaking to the other devs I was told that in our project all injected dependencies are constructed at startup. However not all member objects are already known at startup time. Therefore these members objects can't be passed via constructor.

Another point is that, for each class type (for ex. Bar) only 1 instance seams to be constructed for the whole project, this instance will be passed around in the whole project and accessed by multiple threads. Therefore we might run into concurrency issues if we use member variables.

=>the conclusion in our team was to avoid member variables and pass all arguments as function arguments.

=>Now I'm searching for a way to have DI (which makes perfectly sense) but also to have members variables.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
JHeni
  • 455
  • 3
  • 12
  • 1
    Does this answer your question? [Dependency injection with interfaces or classes](https://stackoverflow.com/questions/10311571/dependency-injection-with-interfaces-or-classes) – Avinash Jun 23 '21 at 11:19
  • 1
    In your minimal example it makes no sense (but then again: the `Bar` class on its own is pointless as it stands). But if `Bar` itself has some dependencies that get injected or can be configured in different ways, then getting it injected de-couples the use of `Bar` from having to configure it correctly. Also: even if it's not an interface, someone could write a `MegaBar` that extends `Bar` and use that to satisfy the `Bar` dependency. – Joachim Sauer Jun 23 '21 at 11:35
  • hm thx that makes sense. However when a bar instance gets injected into several classes, bar cannot have instance members (since the same instance is injected across the project). If I can't have instance members this dost not feel like OOP any more. Am I right? – JHeni Jun 23 '21 at 11:47
  • @Avinash nope sorry this did not answer my question – JHeni Jun 23 '21 at 11:48
  • Why do you think that it cannot have instance members? That seems like an arbitrary restriction that comes out of nowhere. Are you maybe thinking about issues like thread safety? Because having fields does not necessarily make a class not thread-safe. And being injected into multiple places does not necessarily mean that the object must be thread safe. And last but not least: the DI can be configured to create a new instance of `Bar` for each place where it should inject one and the dependent would still not need to know of that. Again: decouple the "how is this created" from "how is it used". – Joachim Sauer Jun 23 '21 at 12:03
  • @JHeni, if that answer doesn't answer your question, please make your question more specific that it becomes different from the referenced question. – Steven Jun 24 '21 at 07:44

1 Answers1

1

When using dependency injection, a class delegates the responsibility to create it's dependencies to the dependency injection system. So you never want to instantiate a dependency yourself.

Otherwise, if your Bar class needs dependencies, you'd have to take care of them as well in MyClass. And if you use Bar in many places and add a dependency to it, you'd have to change all those usages of Bar as well.

ruediste
  • 2,434
  • 1
  • 21
  • 30
  • hm thx for your answer. But (as said in the comment above) what about instance members for my Bar class. Because of dependency injection bar can't have instance members right? – JHeni Jun 23 '21 at 11:49
  • Why do you think that? Of course `Bar` can have members. In fact it can get stuff injected into itself as well. That's pretty much the *point* of DI. – Joachim Sauer Jun 23 '21 at 12:06
  • Hey guys, I clarified my question above and added some project background info why we try to avoid member variables – JHeni Jun 24 '21 at 15:40