In Java, the @Inject annotation is used in the context of dependency injection, typically with frameworks like CDI (Contexts and Dependency Injection) or Spring. It allows for the automatic injection of dependencies into a class, promoting loose coupling and easier testing. Here's a basic example:
import javax.inject.Inject;
public class Car {
private Engine engine;
@Inject
public Car(Engine engine) {
this.engine = engine;
}
public void start() {
engine.run();
}
}
In this example, the Engine dependency is injected into the Car class by the framework, without the need to manually instantiate it.