| Author |
Generics
|
sudharshan tettu
Ranch Hand
Joined: Jul 17, 2006
Posts: 114
|
|
Hi
can anyone explain what is <T> here in the method signature , is that a return type ? am new to using generics
static <T> void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}
when to use and what is need for using that.
Thanks and regards
Sudharsan Tettu
|
 |
Ganesh Gowtham
Ranch Hand
Joined: Mar 30, 2005
Posts: 223
|
|
Hi Sudarsan ,
Usage of <T> is custamisable for any object , not restricted object of any type.
example you will iterate over array a which have object of type T and adds in collection c .
ex1 : array a can have values of type String --> collection should be created to have String .
ex2 : array a can have values of type Perosn --> collection will have all persons from array a after calling fromArrayToCollection(...)
Hope it helps .
|
Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
|
 |
sudharshan tettu
Ranch Hand
Joined: Jul 17, 2006
Posts: 114
|
|
Hi thanks for posting
am actually concerned about the <T> next to static
what is that specify?
static <T> void fromArrayToCollection(T[] a, Collection<T> c) { }
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
It's used to tell that "T" is a generic type, not a real class.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
You can use a generic type on either a class or a method - this is using it on a method. It means the method has been written to accept multiple argument types.
What it means in this case is roughly: "you can call fromArrayToCollection with any array and any Collection as the arguments, as long as the type of the array is the same as the type of the argument".
Does that make sense?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
Have a look at the Java™ Tutorials section, especially the bit about "generic methods . . ." That may help you
|
 |
sudharshan tettu
Ranch Hand
Joined: Jul 17, 2006
Posts: 114
|
|
am really sorry to bug you back... let me post my question the other way...
what difference these two pieces of code make
static <T> void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}
and
static void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}
that is with and without <T> next to static .
|
 |
Dave Lorde
Greenhorn
Joined: Apr 02, 2007
Posts: 20
|
|
The method without the <T> following the 'static' is a generic method in a generic class, where the class has been declared to use 'T' as a generic type. Because the class declares that it's using 'T', the method can just use it without declaring it.
The method with the <T> following the 'static' is a generic method in a class that has not declared 'T' as a generic type. You can have a generic method in a non-generic class. The <T> declares that the method will be using 'T' as a generic type. The method needs to declare this because the class doesn't.
|
 |
Ganesh Gowtham
Ranch Hand
Joined: Mar 30, 2005
Posts: 223
|
|
sudarsan tettu wrote:am really sorry to bug you back... let me post my question the other way...
what difference these two pieces of code make
static <T> void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}
and
static void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}
that is with and without <T> next to static .
static void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}
wont complie at all..
if you have declared class like
public class Hello <T> {
static void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct
}}
}
above code will complie
|
 |
 |
|
|
subject: Generics
|
|
|