From d5bf5a5bf3bf19765236ba36e5e378860d34e800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davor=20Beganovi=C4=87?= Date: Thu, 20 Dec 2018 13:34:53 +0100 Subject: [PATCH] How to create your own generic method (#25467) The syntax how to create your own Generic method. --- guide/english/java/generics/index.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/guide/english/java/generics/index.md b/guide/english/java/generics/index.md index a5e7fe8da1..3ec39972ae 100644 --- a/guide/english/java/generics/index.md +++ b/guide/english/java/generics/index.md @@ -52,7 +52,7 @@ public class Example { } ``` -The syntax to create your own Generic class would be as follows. +**The syntax to create your own Generic class would be as follows.** ```java import java.util.ArrayList; @@ -81,8 +81,17 @@ Note that the letter `T` is a placeholder, you could make that anything you like throughout the class. +**The syntax to create your own Generic methods would be as follows.** +```java +public class GenericMethod { + public static void printObject(T t){ + System.out.println(t.toString()); + } +} +``` -Type Erasure in Java Generics + +## Type Erasure in Java Generics One of the downsides to introducing generics in Java 1.5 was how to ensure backward compatibility (ie) to make sure existing programs continue to work and all existing libraries must be able to use generic types. @@ -135,4 +144,5 @@ public class GenericsErasure When you compile some code against a generic type or method, the compiler works out what you really mean (i.e. what the type argument for T is) and verifies at compile time that you're doing the right thing, but the emitted code again just talks in terms of java.lang.Object - the compiler generates extra casts where necessary. -At execution time, a List and a List are exactly the same the extra type information has been erased by the compiler. \ No newline at end of file +At execution time, a List and a List are exactly the same the extra type information has been erased by the compiler. +