The Artima Developer Community
Sponsored Link

Java Answers Forum
Generics Help

4 replies on 1 page. Most recent reply: Jan 12, 2009 6:03 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Kalpak Gadre

Posts: 1
Nickname: kalpakrg
Registered: Dec, 2008

Generics Help Posted: Dec 16, 2008 9:40 AM
Reply to this message Reply
Advertisement
How can I make following code compile?


import java.util.ArrayList;
import java.util.List;


public class GenericsTest {

public static void main(String args) {
Field<String> f1 = new Field<String>("test");
Field<Integer> f2 = new Field<Integer>(1);
Field<Long> f3 = new Field<Long>(1l);

List<Field<?>> fields = new ArrayList<Field<?>>();
fields.add(f1);
fields.add(f2);
fields.add(f3);

for (Field<?> field : fields) {
Object value = field.getValue();
// Compilation error! Expects specific type.
field.doSomething(value);
}
}

private static class Field<T> {
private final T value;

public Field(T value) {
super();
this.value = value;
}

public T getValue() {
return value;
}

public void doSomething(T val) {
System.out.println("Value: " + val);
}
}
}


Is there any special syntax to cast the value passed in field.doSomething(value) to the respective generic type?

Note that the example I have given is just to describe my problem. In the actual scenario, the value is not obtained from Field object itself, I use the Field object to obtain the value from Message object and its not accessible locally.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Generics Help Posted: Dec 23, 2008 7:35 AM
Reply to this message Reply
Without looking deep at all, the first glaring problem is that there is no code defining the T class.

I don't see anything like

public class T{
//T methods
}

Andrea Andenna

Posts: 2
Nickname: penna
Registered: Nov, 2005

Re: Generics Help Posted: Jan 11, 2009 12:41 PM
Reply to this message Reply
Hello Kalpak,

I did not fully understand your design.
Nevertheless, if you remove the wildcard in the for loop like this:
    ...
    for (Field field : fields) {
    ...

then it compiles and does the right thing.

Regards
Andrea

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Generics Help Posted: Jan 11, 2009 10:01 PM
Reply to this message Reply
> Without looking deep at all, the first glaring problem is
> that there is no code defining the T class.
>
> I don't see anything like
>
> public class T{
> //T methods
> }

That's not an issue.

public class A<T>{
}


Is perfectly legal, all its saying is that make sure you type it when referencing the class.

Haven't gone through the code either. Will take a look.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Generics Help Posted: Jan 12, 2009 6:03 AM
Reply to this message Reply
Thanks for that.

Flat View: This topic has 4 replies on 1 page
Topic: i= i++ + ++i + i++ + ++i Previous Topic   Next Topic Topic: Can anyone help explain/comment on this code ?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use