Java Generics and Typing from reflection

Posted on 2006-03-29
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


    Author Comment

    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

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now