This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static <T> void myMethod (T [] a Collection <T> c) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static <T> void myMethod (T [] a Collection <T> c)" Watch "static <T> void myMethod (T [] a Collection <T> c)" New topic
Author

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

Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
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....
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

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

Joined: Feb 24, 2009
Posts: 268
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

Joined: Apr 30, 2003
Posts: 820

Deepika Joshi wrote:

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



you and me both
Mark Moge
Ranch Hand

Joined: Mar 14, 2009
Posts: 96

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.


SCJP6 | http://programmerutilities.blogspot.com/
Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
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
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
[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.

Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
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");

Rajeev Trikha (SCJP 6)
 
Don't get me started about those stupid light bulbs.
 
subject: static <T> void myMethod (T [] a Collection <T> c)
 
Similar Threads
get Property-Field-Name via Reflection
Generics: lower bound method signature
Generic methods
Generics
Strange recursive method call! ???