Java EE 7 Application Developer Exam: @Inject Annotation Limitations

Elements Not Supported by @Inject in Java EE 7 Application Development

Question

Which two elements CANNOT be injected by using an @Inject annotation? (Choose two.)

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

BD.

https://docs.oracle.com/javaee/6/api/javax/inject/Inject.html

In Java EE, the @Inject annotation is used for dependency injection, which allows objects to be created and their dependencies to be injected into them automatically. However, there are certain limitations to what can be injected using this annotation.

The two elements that cannot be injected using @Inject are:

A. Static Fields: Static fields are associated with the class and not with a particular instance of that class. Therefore, they cannot be injected with values specific to an instance, and it makes no sense to inject them using @Inject.

B. Instance Fields Declared Final: Final fields cannot be modified once they are initialized. If they were injected using @Inject, the injected value would be unable to modify them, which would render the injection pointless. Therefore, @Inject cannot be used with instance fields declared final.

C. Concrete Methods: Concrete methods are regular methods that have a body and are not abstract or interface methods. These methods are not injected using @Inject, but rather they are called by other methods that are injected.

D. Abstract Methods: Abstract methods cannot be instantiated, and hence cannot be injected using @Inject. Abstract methods can only be implemented by concrete classes that are instantiated, and the dependencies of those classes can be injected using @Inject.

In summary, only non-static, non-final instance fields and concrete classes can be injected using @Inject. Abstract classes, abstract methods, final instance fields, and static fields cannot be injected using this annotation.