Good and complicated and reliable way to sort it out. You can put -Xlint on as much as you like, and the compiler won't find anything to complain about, but you will probably use the cheap and cheerful method because this way to do it is complicated.
This will work in Java5 and later, not in Java1.4.2 or earlier.
Parameterise the list.
Every method in the LLObjectNode class which takes an Object as its Parameter takes an E. That's right, an E. Short for element.
Every method in the LLObjectNode class which returns an Object as its return type returns an E.
The type of class is changed; it is no longer LLObjectNode, but LLOBjectNode<E>.
Every time you quote the LLObjectNode class you have to quote it as LLObjectNode<Something>, and you add the same <Something> to any constructor calls.
What that means is the compiler will only allow you to put one particular kind of Object in, and only get one kind of Object (the same kind) out. If you declare the formal type parameter as <Integer>, then you can only put Integers in, and only get Integers out.
This is a brief introduction to generics
, which ought to have been in the language from day 1 and was introduced 8 years too late in September 2004 (Java
So how do you get an Integer into an int? Another feature introduced in Java5 was boxing (also called auto-boxing because it is done automatically). What it means is you can present an int to something which is an Integer and the compiler will fiddle it so it works. It works the other way round, too, so you can have an Integer and put it into an int variable, only that is called unboxing.
So, parameterising the entire class and then using it with <Integer> will sort that problem out.