0

I really can't explain this one. I have a Java List<Byte> myList; that is set like this:

class AJavaClass {
    // ...
    private List<Byte> myList;
    public AJavaClass(List<Byte> myList) { this.myList = myList; }
    // ...
}
AJavaClass aJavaClass = new AJavaClass(customMongoRepository.customFind());

The customMongoRepository object is injected via Spring. It is defined like this:

public interface CustomMongoRepository extends MongoRepository<AComplexClass, String> {
    List<Byte> customFind();
}

However, when looking inside aJavaClass during execution, myList is actually of type List<AComplexClass>.

Now, what I understand is that the query executed in customFind doesn't yield a List<Byte>, but rather a List<AComplexClass>. What I don't understand is how this doesn't throw an exception, and how a variable can contain a value of a different type.

Rosh Donniet
  • 418
  • 2
  • 10
  • Sounds like you need to learn more about what Java does with generics. http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#Why do instantiations of a parameterized type share the same runtime type? – Jon Skeet Jan 23 '17 at 12:28
  • During execution, all lists are `List` (or, to be more correct, just `List`). Check "type erasure" http://stackoverflow.com/questions/339699/java-generics-type-erasure-when-and-what-happens. And because of this, generics are for compile time checks only. – SJuan76 Jan 23 '17 at 12:28

0 Answers0