File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to get getClass using Generics a Newbie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get getClass using Generics a Newbie" Watch "How to get getClass using Generics a Newbie" New topic
Author

How to get getClass using Generics a Newbie

Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
hi guys how is everything. i have a question i will try to be explicit.

i have a class something like this.



i have another class(external).



how i could know. in mymethod method. which class myClass really is[student] in this case.

something like getClass.

thanks a lot. sorry by my poor english.

Alexander Kober
Ranch Hand

Joined: Aug 05, 2011
Posts: 32

You can, to some degree, use the generic parameter which you called 'T'. That is, you can do something like this in myMethod:



You can not, however, instanciate objects or arrays of T, or get the class of T. Unfortunately, java has what is called type erasure. Effectively, this means that while the compiler will check the type of MyClass at compile time, the information will be lost at runtime. This has been criticized by a lot of java developers. Java 8 will probably be able to preserve the type parameter at runtime, as do other JVM languages like Scala, but with pure Java 6/7, you can't do what you want.
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Alexander Kober wrote:You can, to some degree, use the generic parameter which you called 'T'. That is, you can do something like this in myMethod:



In Java 7 Is not Expected???
Alexander Kober
Ranch Hand

Joined: Aug 05, 2011
Posts: 32

Afaik type erasure is still preset in Java 8, and there is no solid commitment that it will be removed for Java 8.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get getClass using Generics a Newbie
 
Similar Threads
Strange recursive method call! ???
is it valid to declare an inherited method as abstract?
declaring an array using an interface type
about variable persistence...
pl help - urgent