Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get a generic class' class?

 
Saverio Miroddi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd want to create a generic class, which I dould use the generic parameter in, both in a method return and to get the class' name.

i haven't succeeded in accomplishing the 2nd. Suppose i have this:


class SomeClass<C> {
public C someMeth() {
// this is OK
}

public void someOtherMeth() {
---> out.println(C.getSimpleName()); // this isn't possible!
}
}

why isn't possible to call any C class method, even if C parameter it's supposed to be a real class?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that calls to a static method are resolved when the calling code is compiled; when compiling a generic class, you don't know what that class is, so there's no way to check if it's a valid call. In any event, Java has no mechanism by which the "real" class could be substituted at runtime; remember that generics are a compile-time thing, and only one class is generated for the generic class, no matter how many times it is instantiated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic