• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

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.
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic