• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static <T> void myMethod (T [] a Collection <T> c)

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static <T> void myMethod (T [] a Collection <T> c)


I am learning generics and I am not sure role of <T> next to 'static'...

Can you please write how to read it (static <T>), and what is use of <T>

Thanks....
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are missing a comma in the method arguments, but...
<T> in this case means that you can use it in the method to refer to the type object you are passing in the arguments.



in the code above, the ability to use "T" like that comes from the <T> next to static
 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static <T> void myMethod (T [] a, Collection <T> c) {
}

you are correct, and I got your point... I have read few pages on this but still finding it to difficult digest ....

thanks....
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepika Joshi wrote:

I have read few pages on this but still finding it to difficult digest



you and me both
 
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The <T> before void defines what T is before you use it. You have to tell compiler what type of an argument you want to use. And if you are using generics methos thats the syntax.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepika Joshi wrote:static <T> void myMethod (T [] a Collection <T> c)


I am learning generics and I am not sure role of <T> next to 'static'...

Can you please write how to read it (static <T>), and what is use of <T>

Thanks....




Oh that is what i just studied [still i'm not perfect, but can give you a clue]

T stands for "type" & E stands for "element"

Now if you make a generic class than you will declare the type in the class itself as


but sometimes you don't need to make a generic class and you only need to make generic method, that is when you have to declare your method in such a way so that you can place the type element of class in your method:

declaration of generic method for the above code:



<T> must come before return type.

regards,

Rafi
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[code]
static <T> void myMethod (T [] a, Collection <T> c)
,[code]

"T" is just a java identifier. We can have any valid java identifier in place of "T".

This type of syntax of a method implies that it is a generic method. you can call this method with any type of patameters. "T" can be taken over by int, float, short , double or any other object.

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is also worth knowing that Type parameters cannot be used in a static context, and thus cannot be used with static methods or variables, or enums. Also Exceptions and anonymous inner classes cannot be generic classes.

Generics have an optional syntax for specifying the type for a generic method call. You can place the data type of the generic in angle brackets, < > , after the dot operator and before the method name ie obj.<String>method("String");
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic