• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Generics ? declaration spot

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Can't we declare ? in class level.. Is it used only in method declarations.. Here is the sample code.
 
Saloon Keeper
Posts: 10308
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use it in method declarations either. You can only use it in declaring variables, which is what you're doing in the signature of your test method.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We cannot add anything within the method because of ?. But, we are able to use the ? in method declaration. I want to make sure if we can declare ? in it in class level or not.
 
Stephan van Hulst
Saloon Keeper
Posts: 10308
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't declare a class with a wildcard parameter no.

But you also can't declare a method with a wildcard parameter.

What you did was declaring a *variable* with a wildcard parameter, which is always valid.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you also can't declare a method with a wildcard parameter. --> I think you are saying something like this is NOT allowed.
public void test(?) {}
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But you also can't declare a method with a wildcard parameter.


You cannot have a generic method where a wildcard is used to define a type parameter. Say like this

The method defined by you is a non-generic method that makes use of generic variables.i.e it does not define a type parameter.

Same is the case for generic classes. One can't use wildcards to define type parameters.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!