Links & DependenciesCar.javaEngine.javaWheels.javaMainActivity.javaCarComponent.java

Dagger 2 dependencies:

github.com/google/dagger

package com.codinginflow.dagger2example;

import android.util.Log;

import javax.inject.Inject;

public class Car {
    private static final String TAG = "Car";

    private Engine engine;
    private Wheels wheels;

    @Inject
    public Car(Engine engine, Wheels wheels) {
        this.engine = engine;
        this.wheels = wheels;
    }

    public void drive() {
        Log.d(TAG, "driving...");
    }
}

package com.codinginflow.dagger2example;

import javax.inject.Inject;

public class Engine {

    @Inject
    public Engine() {

    }
}

package com.codinginflow.dagger2example;

import javax.inject.Inject;

public class Wheels {

    @Inject
    public Wheels() {

    }
}

package com.codinginflow.dagger2example;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    private Car car;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CarComponent component = DaggerCarComponent.create();

        car = component.getCar();
        car.drive();
    }
}

package com.codinginflow.dagger2example;

import dagger.Component;

@Component
public interface CarComponent {

    Car getCar();
}