Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Generic class

843793Apr 8 2010 — edited Apr 8 2010
Hi,

I have this following class with a get method:
public class Dao {
    <T> public T get(Class<T> clazz, long id) {
        ...
    }
} 
Now, what I want is to create a dao that takes a specific model class. I don't want it's users to have to send the class as argument to the get method, and would like the code to look like this:
public class SpecificDao<T> {
    private Dao dao;
    public T get(long id) {
        return dao.get(/* ??? */, id);
    }
}
So my problem is, what should I use instead of /* ??? */. I can't write T.class since no such thing exists. But I need the Class instance of T. Any ideas on how to retrieve it?

Thanks,

/U

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 6 2010
Added on Apr 8 2010
2 comments
179 views