Is my definition of a generic method in Java ok?

Here's my basic definition of a generic method. Is it acceptable?

Generic Method - a method in which type parameters appear within angled brackets, right before the return type. The code below is a generic method that uses an enhanced for loop to add array elements to a collection one by one. The third line of the code would not have compiled if the <T> at the beginning of the method signature was not there. That <T> made it possible for the array and the collection in the method parameter to be typed the same generic way, so that this method would be compatible with any object type.

static <T> void fromArrayToCollection(T[ ] a, Collection<T> c) {
     for (T o : a) {
        c.add(o); // adding an array element of type T
     }
}
shampouyaAsked:
Who is Participating?
 
for_yanCommented:
And about "T" in front:

Compiler has to account for any entity when it parses the code - each entity is either a class or variable name or something
which compiler should yunderstand.
If you don't put "T" in front - it will parse "T" inside the metghod definition - and will not know what it is - when it
saw "T" in front of static it understandts that it is a generic method and will
be able to recignize what is "T" when it parses the contents of the method.

By the way you can use almost arbitrary striung instead of "T" - I replaced "T" by "MyT" in all places and it also works - it is just a tradition to use "T" for "type"
0
 
for_yanCommented:
Yes, this seems fine to me
0
 
for_yanCommented:
if it compiles then it is fine
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
for_yanCommented:
And it does compile fro me
0
 
for_yanCommented:
Although my IDE suggest to replcae it withh Collection.addAll method, but this does not matter; once it compiles it measn it is correct
0
 
shampouyaAuthor Commented:
And the <T> after the word static is what turned this method into a generic method, right?
0
 
for_yanCommented:
Yes, without that "T" in fromt of static compiler would not understdn what means T inside the mthod and in parameters
0
 
for_yanCommented:
So this code compiles fine together with your method definition:

  
        Collection<String> cc = null;
        String [] cmcm = null;
        
            fromArrayToCollection(cmcm, cc);
        

Open in new window

0
 
shampouyaAuthor Commented:
Oh I know it compiles, correctly, I took it from the Oracle website. I was just wondering if my english was correct when I defined a generic method. And I was wondering if the <T> before the return type is what can turn a regular method into a generic method.
0
 
for_yanCommented:

Aslo this compiles:

        Collection<Object> cc = null;
        String [] cmcm = null;

            fromArrayToCollection(cmcm, cc);

Open in new window


but this does not:


Collection<String> cc = null;
        Object [] cmcm = null;

            fromArrayToCollection(cmcm, cc);

Open in new window


This is in compliance with your previous
statements about covariance of arrays and collections
0
 
shampouyaAuthor Commented:
I'm confused and I'm not sure why you are telling me what compiles.That wasn't my question....
0
 
for_yanCommented:
I just liked that your talks about covariance of arrays vs collections in previous question are nicely illustrated by this example
 
0
 
shampouyaAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.