• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Generics - static variables and methods of a class are shared among all the instances

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading the Java Generics tutorial and came across the following line -

As consequence, the static variables and methods of a class are also shared among
all the instances. That is why it is illegal to refer to the type parameters of a type
declaration in a static method or initializer, or in the declaration or initializer of a static
variable.



What does the line in bold mean? I am not able to exemplify it.

Vijay.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The type parameter T

in


gets a concrete type only after an instantiation like new Hello<Integer>();

But you know static methods can be accessed even when you don't have an object of the class.

So if you had used the T in the aMethod, the compiler would not know the concrete type to substitute in place of T.

HTH,
Vishwa
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a little tricky. You get an error if you try to compile this too:

You could argue that this should work, since you might do
StaticGenerics<Integer>.a = 10;
and the compiler should have no problem inferring T as being Integer. The problem is that somewhere else you might have
StaticGenerics<String>.a = "Text";
Then you have a conflict.
You can have instance variables of type variable types, because each instance will be tied (like Vishwanath said) to a specific generic type, since you can't have an instance variable without having an instance of the class, and when the class is a parameterized type, it means you have provided a concrete type for the type variables, so the compiler can infer the concrete type for the type variable. But when you deal with static stuff the compiler can't do that and ensure there won't be conflicts.

In other words, for this generic type:

StaticGenerics<Integer> instances seem to get an Integer b member
StaticGenerics<String> instances seem to get a String b member
(notice I say "seem to get" and not "get" because in reality what they get is an Object b member due to type erasure, but as a part of type erasure the compiler will insert casts to the specific type as necessary.)
But
StaticGenerics<Integer> class can not get a static Integer a member
StaticGenerics<String> class can not get a static String a member
(the reason why this can't work is that static members are shared by all instances, and since you can have instances of more than one generic type, the compiler has no way of performing type erasure.)
What would happen if you set a like this:
StaticGenerics<Integer>. a = 10;
and then try to access it like this:
String = StaticGenerics<String>.a;
 
Wanna see my flashlight? How about this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic