Learn how to a build a cloud-first strategyRegister Now


Java Generics and Typing from reflection

Posted on 2006-03-29
Medium Priority
Last Modified: 2008-02-26
Is it possible to have a class declared with a generic, and then create an instance of this class by passing the
type of a specific object as the generic parameter ? For example, consider a class defined as

public class SampleGeneric< T > {
      private T _member;
      public void setMember( T obj ) {
            _member = obj;
      public T getMember() {
            return( _member );

Now, normally, I would instantiate this as

SampleGeneric< Integer > sg = new SampleGeneric< Integer >();

but I am interested in instantiating the SampleGeneric class based upon the type of an object....

AnotherType at = new AnotherType();
SampleGeneric< GetType( at ) > sg = new SampleGeneric< GetType( at ) > ();

where GetType( at )  is just my pseudocode for whatever is in the Java Reflection API to do this job.

Can I do this, and if so, how ?

Question by:jmuldrum
LVL 30

Accepted Solution

Mayank S earned 1500 total points
ID: 16323743

Author Comment

ID: 16324772
The Sun java tutorial doesn't specifically address my issue. It seems to say that what I need cannot
be done directly because of erasure. I understand that erasure is the reason why I cannot cast using
a generic parameter,  "return(  <T> result ); ", but I am not sure if it eliminates what I am trying to do.
I think that the most important piece of information I am missing is whether or not Java has the ability
to get the type of an object the same way that C# does ( obj.GetType() ).

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question