• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get getClass using Generics a Newbie

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 32
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic