Is the following a valid example of Dependency injection.
public class Employee { private String name; private String company; public Employee(String name, String company){ this.name = name; this.company = company; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getCompany(){ return company; } public void setCompany(String company){ this.company = company; } } Application class has dependency on Employee
public class Application { private static Employee emp; private static String name; private static String company; public Application(Employee emp){ this.emp = emp; } public static String getApplication(){ name = emp.getName(); company = emp.getCompany(); return "Name: " + name + "\nCompany: " + company; } public static void main(String[] args) { // TODO Auto-generated method stub Employee emp1 = new Employee("John", "ABC"); Application app1 = new Application(emp1); System.out.println(app1.getApplication()); } }
private static Employee emp;... static.... no, it is not.staticexcept on yourmainmethod.