Results 1 to 6 of 6
Thread: Casting to an List<Serializable>
- 10-15-2009, 06:05 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
Casting to an List<Serializable>
Good afternoon,
I am trying to return a List<Serializable> from a method
where the method getDbDescriptors is:Java Code:public List<Serializable> getDBInfo() { return ApplicationConfiguration.getInstance().getDbDescriptors(); }
but it is complaining aboutJava Code:public List<DBDescriptor> getDbDescriptors() { return this.dbDescriptors; }
so I figured I would just cast it to the type expected:Java Code:Multiple markers at this line - Type mismatch: cannot convert from List<DBDescriptor> to List<Serializable> - Cannot cast from List<DBDescriptor> to List<Serializable>
but it does not like that either.Java Code:return (List<Serializable>)(ApplicationConfiguration.getInstance().getDbDescriptors());
Am I missing something obvious here or is this not possible.
I could of course just iterate and create a new list but really don't want to create a new object unless I absolutely have too.
Thanks in advance.
Whatty
-
I'm no pro at generics, far from it, but what about doing,...
Java Code:public List<? extends Serializable> getDBInfo() { return ApplicationConfiguration.getInstance().getDbDescriptors(); }
- 10-15-2009, 07:05 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
The error message is very explicit. Those two are not the same. Why did you expect it to work?
- 10-15-2009, 07:18 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
I just read the following whitepaper on Generics and wow!
Generics Tutorial (Generics in the Java Programming Language) - http: //java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
However, your suggestion worked, as example:
Java Code:public List<? extends Serializable> getSerializedDBInfo() { return ApplicationConfiguration.getInstance().getDbDescriptors(); } @SuppressWarnings("unchecked") public void serializedDBTest() { List<Serializable> results = (List<Serializable>)this.getSerializedDBInfo(); for (Serializable serial : results) { // do something } }
to R035198x - yes, you are correct, it is very explicit (thanks I was unaware of that) but already tried to cast it.
Help instead of flaming someone is a much better approach but that IMHO.
Whatty
- 10-15-2009, 07:44 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
I wonder what gave you the idea that I was trying to flame.
You never mentioned that DBDescriptor is in fact Serializable so it seemed strange to me that you would want to substitute one for the other.
The good thing is that at least you read the generics tutorial. It's usually pointless to try to use a feature without first reading a relevant tutorial about that feature.
P.S ignoreList++;
- 10-15-2009, 07:47 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Casting
By zzpprk in forum Advanced JavaReplies: 13Last Post: 08-13-2009, 07:59 PM -
casting help
By soc86 in forum New To JavaReplies: 4Last Post: 01-13-2009, 11:07 PM -
Concurrent timers are not getting invoked for same timer info (Serializable object co
By Neeraj in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-31-2008, 02:20 PM -
Implementing Serializable interface
By javaplus in forum Advanced JavaReplies: 4Last Post: 12-18-2007, 12:29 PM -
Casting
By leebee in forum New To JavaReplies: 5Last Post: 08-10-2007, 12:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks