You do not need it, this is a sort of default.
I'd say, wrinting only
bind(MyClass.class);
does what you wanted.
> And this is not the same thing:
>
> bind(MyClass.class).toInstance(new MyClass())
>
> because that instance will not have been supplied with an
> AnotherClass.
Right. Generelly, I try to avoid bind-toInstance.
In the case above annotating MyClass with @Singleton or using simply
bind(MyClass.class).asEagerSingleton()
or maybe
bind(MyClass.class).in(Scopes.SINGLETON);
may be better (I supose you want a singleton since you get one using bind-toInstance).
> I could have some sort of bootstrap where I:
>
> bind(AnotherClass.class).toInstance(new AnotherClass());
>
> and then
>
> Injector injector = Guice.createInjector(getBootstrapModule());
> bind(MyClass.class).toInstance(injector.getInstance(MyClass.class));
>
> but then I'm stumped if I want to then inject both MyClass and
> AnotherClass into something, unless I can live with multiple instances
> of AnotherClass.
I think this could be solved using child injectors but this is currently too high for me.
> I do not want to write interfaces for these classes just so I can
> inject them; have I got something wrong here, or is there a way around
> this?
You need no interfaces for this. Generally, you can use abstract or concrete superclass instead of interface, and you need nothing if there's no hierarchy.
> Many thanks if you are able to help
Regards, Maaartin.
While it is not recommended practice, sometimes you might need to bind a concrete class without having an interface to which to bind it."