Это происходит потому, что класс String
предоставляет и empty constructor
. Когда Guice видит, что String вводится, он пытается найти привязку для String или вызывает пустой конструктор класса String.
Из Java кода класса String
/**
* Initializes a newly created {@code String} object so that it represents
* an empty character sequence. Note that use of this constructor is
* unnecessary since Strings are immutable.
*/
public String() {
this.value = "".value;
}
Как видите, оно устанавливает значение ""
. По этой причине вы видите, что пустая строка печатается на выходе.
Попробуйте то же самое, например, с Integer
, и вы получите
Could not find a suitable constructor in java.lang.Integer. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. at java.lang.Integer.class(Integer.java:52)