Skip to content
Advertisement

Tag: generics

Java enum with generic attributes

I have been trying to create enum that contains generic attribute, for example: SomeValue1 and SomeValue2 classes implement SomeValue interface. For some reason <T extends SomeValue> is marked with “Unexpected bound” error. If I replace T with ?, there is no error. But it is still puzzling me why it is happening when I use T. Any help would be

Casting Issue for Generic Array of Node Objects

I am having problem with java generic array creation where I needed to make an array of type Node. So, I did this for declaration: private Node<E> [] nodes; and later for initialization, nodes = (Node<E>[]) new Node [values.length]; When I try to do something like set the Node object’s attribute value nodes[i].setValue(values[i]); , I get NullPointerException, meaning that there

Java Generics – quick question on Greater Than Method with Objects

I have an assignment in my Java class, we are learning Generics, I have looked in my notes, my lessons, and even on the internet and I still cant figure out what this last assignment question is asking me to do, which is: Write a generic “greater-than” function that takes two objects as arguments, each of which has a “value”

Java generics impossible assignment?

Every time I think I understand generics better (and can answer without compiling), I get to an example where this theory breaks. Here is a very simple example: And two invocations: To me, none of the invocations should compile. A String is not a supertype of List. Still, the second one compiles. But let’s suppose that this happens because the

Advertisement