Skip to content

Tag: generics

Making method generic and attributes

I am using quite a similar method for 4, 5 screens basically to do same thing on List of different objects. Now I extracted that method to reuse it and make it generic. Now while making a list generic I face the issue with attributes. It’s attributes are no longer accessible when made generic. How to ov…

Why java lets me add a Box(Raw type) to a List<Box>

This code works, and I dont understand how the list is able to add a raw type box, I thought type erasure will set the type to their indicated bounds. note: Paper class doesn’t extends Bakery class. EDIT: Is my understanding correct? the compiler typecasts the raw box type so that it can be added to the…

Is there a way to see what my code looks like after type erasure?

I would like to know what tool can be used to view code after type erasure. For example, I’d like to see what the following code looks like after type erasure. I know that generic type information is stripped prior to final compilation, replacing type references with Object or boundary types, creating b…

How is compiler inferring type on a generic method?

I have a Storage class: There are two classes where class Child extends Parent: Parent Child Driver class: Because Storage is a generic class, the operations on its method makes sense; I get how case 1 is working. In case 2, with above signature of max in GenericTest, I get the compilation error: The method m…

Nested generic types in static method

I was trying to develop a generic method that could convert a JSON to a generic object that would have another instantiable generic object inside so I could use it as a parser in several places in my application. I had thought of the following solution, but it doesn’t work: Is there any way to be able t…