File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Accessing a static variable from a generic 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 "Accessing a static variable from a generic class?" Watch "Accessing a static variable from a generic class?" New topic
Author

Accessing a static variable from a generic class?

Sidharth Panwar
Greenhorn

Joined: Apr 30, 2006
Posts: 8
class MyGeneric<T>
{
static int i;
---- Other stuff ----
}

class TestGeneric
{
public static void main(String[] args)
{
System.out.println(MyGeneric<String>.i);
}
}

Gives a compile-time error:
illegal start of expression - System.out.println(MyGeneric<String>.i);
^

What is the problem?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Generics are a compile-time artifact. In your example there is only one "MyGeneric" class at run-time and only one instance of the static variable "i". Mentioning the class parameter when trying to access it is pointless and hence wrong.
Sidharth Panwar
Greenhorn

Joined: Apr 30, 2006
Posts: 8
Originally posted by Garrett Rowe:


Thanks Gerrett, didn't think about trying it this way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing a static variable from a generic class?