File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get getClass using Generics a Newbie

 
Cristian Daniel Ortiz Cuellar
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic