This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
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
Sheriff

Joined: Oct 14, 2005
Posts: 19434
    
  10

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.
 
 
subject: Accessing a static variable from a generic class?