Java Generics and Typing from reflection

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 ?

Thanks
jmuldrumAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmuldrumAuthor Commented:
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() ).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.