Is it possible to use generics in lists in Java 1.4?

angryITguy picture angryITguy · Jun 12, 2011 · Viewed 8.4k times · Source

I have to use Java 1.4 and am using arraylist structures. Now I need to do some re-factoring and it would help if I can use Generics. Currently I have code like this

ArrayList rows = new ArrayList();

Which is a straightforward ArrayList. But now the "rows" variable is becoming more complex and need to define a "Row" class. I now want to do something like what you see in J2SE 1.5

ArrayList<Row> rows = new ArrayList();

A trawl through Google doesn't expose any answers without some sophisticated use of 3rd party solutions. How would my code have to change to accommodate this in 1.4, without using 3rd party solutions/open source projects, (if possible)?

Answer

Alex Gitelman picture Alex Gitelman · Jun 12, 2011

Generics were introduced in JDK 1.5. So you can't use them in 1.4. However, you may be able to use JDK 1.5 compiler but target 1.4 with your classes with -target 1.4 javac option while keeping -source 1.5 option to indicate that your source is 1.5 compatible. In this case you can use generics as they should not affect result classes.

See the section on Cross-Compilation Options