aspose file tools*
The moose likes Java in General and the fly likes Get a generic class' class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Get a generic class Watch "Get a generic class New topic
Author

Get a generic class' class?

Saverio Miroddi
Greenhorn

Joined: Jun 16, 2006
Posts: 21
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?


Using a text editor for programming.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get a generic class' class?