• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Generics - wildcard?

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I don't understand that...
Can I use wildcard on type of class definition???


public class Teste<T,X extends Number>{

public static void main (String args) {

Teste<String, ? super Integer> teste = new Teste<String, Number>();


}
}

 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm so cofused.

What's the meaning of to define a class like

public class Teste<T,X extends Number>{

and later use like this:
Teste<String, ? super Integer> teste = new Teste<String, Number>();

What I can and I can't to do with the second argument?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to think about the two thinks completly separated one from another:
One is defining a class Tester, the other defining a variable of type Tester

When defining a class like

you declair that every instance of this class will have to have 2 parameters (of cause you can leave the parameters at all, but if paramters are used...).
The first has to be whatever the implementer wants.
The second has to extend Number , so could be Integer or Long or...

This guarantees that Methods of tester are acting only on Number-Instances, not e.g. on Strings..


If than someone instantiates your class

she is doing 3 things:
a) declair a variable named teste.
This can hold every Teste-object which has parameters String ans Something superior to number.
b) create a new Teste-Object with Parameters String and Number
c) assign the object to the variable

Due to your class-definition the object could also be a
new Teste<Integer,Integer>()
but you would not be able to assign to the variable.

On the other hand, later inb the program, you could instantiate another tester-object eg (new Tester<String,Integer>()) and assign to your variabe tester.
This is ok cause Integer is "super" to Integer.

Other Instance-types are not possibel to assign to tester,because
var 1 has to be a String and
var 2 has to be super to Integer and extending Number and this is true only for Number and Integer

Hope this helps
 
    Bookmark Topic Watch Topic
  • New Topic