• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Do I Get an Object of a parameterized Class?  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 223
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write a Java program that reads records from disk, sorts them, and then writes them in sorted order back to disk, and does it (hopefully) very quickly. I don't care what type the records are, as long as they extend {Comparable}, and I don't care how they get read from disk or written back to disk, as long as the I/O class extends my interface {InputOutput} defined like so:

So I wrote my code like follows:

When I try to compile this I get told:

HorseFly.java:40: error: unexpected type
IO InpOutp = new IO();
^
required: class
found: type parameter IO
where IO,EL are type-variables:
IO extends InputOutput<EL> declared in class HorseFly
EL extends Comparable<EL> declared in class HorseFly

I need to create an Input/Output object somehow, so I can read my {EL} elements from disk and write sorted {EL} elements back to disk. If I can't call a constructor to get such an object, how can I get it?

Kevin S
 
Simon Roberts
Author
Ranch Hand
Posts: 182
9
Java Linux Netbeans IDE Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The generic types in Java are compile time features only, used for consistency checking. Your problem, I believe relates to "non reifiable types".

This will probably help you understand this. http://docs.oracle.com/javase/tutorial/extra/generics/fineprint.html

HTH

 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry to be so blunt, but this code is really messy. Your variables are unreadable and your methods names don't seem to match their purposes. Instantiate your class with readers and writers. Decouple responsibilities:


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!