Home Java Java-methods Generics Method in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Generics Method in Java

Advertisement
After going through the example, you will be able to declare and use generic methods in Java programming language.

Generics Method in Java

     

After going through the example, you will be able to declare and use generic methods in Java programming language. As you already know, generic method use parameter type declaration and that can be done using the <> syntax. 

For example:

public static <E> void printArray(E[] inputArray). The syntax can be used only before the return type of the method. Basically generic method can be used by any generic or nongeneric class and those class can used that generic type as argument. They can also use it as return type.

 

 

 

 

Code for Java Method - Generics

public class UseGenericMethod {
public static <E> void printArray(E[] inputArray) {
for (E element : inputArray)
System.out.printf("%s ", element);
System.out.println();
}
public static void main(String args[]) {
Integer[] array = 102030405060 };
System.out.println("The Array contains:");
printArray(array)
}
}

Output will be displayed as:

Generic Method Java

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 15, 2008

Related Tutorials

Discuss: Generics Method in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
RAJESH.T
August 23, 2011
java

please explain the function overloading concept
Twumasi Emmanuel
April 10, 2012
no subject

no comment
DMCA.com