0Pricing
Java Academy · Lesson

Generic Methods/Classes

Define generic classes with type parameters and write generic methods with <T> before the return type. Learn where type inference helps.

Generic Methods/Classes is a free Java Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Overview

Generic classes reuse one design for many types, and generic methods work with type parameters even inside non-generic classes. We will create a tiny Pair<T,U>, a swap method, and a static factory.

Why generic classes?

Neden sınıfı generic yaparız?

  • Tek tasarım, çok tür: tekrarı azaltır.
  • Tür güvenliği: yanlış türler derlemede yakalanır.
  • Temiz API: dönüşlerde cast yok.

Code: Pair<T,U>

Pair<T,U> aynı sınıfı iki farklı türle kullanır. Cast gerekmez, API nettir.

public class Main {
  // Simple generic data holder with two type parameters
  static class Pair<T, U> {
    private final T first;
    private final U second;
    Pair(T first, U second) { this.first = first; this.second = second; }
    T first() { return first; }
    U second() { return second; }
    public String toString() { return "Pair(" + first + ", " + second + ")"; }
  }

  public static void main(String[] args) {
    Pair<String, Integer> p = new Pair<>("age", 21);
    System.out.println(p);
    System.out.println(p.first() + " -> " + p.second());
  }
}

Generic methods: signature

Generic metotlar sınıfın generic olmasından bağımsız çalışır. İmza: <T> returnType... Örnek: <T> T firstOrDefault(T[] arr, T def).

Code: generic swap

<T> metot seviyesinde tanımlandı; böylece farklı dizilerle çalışır. Çağırırken tür belirtmedik, derleyici çıkarım yaptı.

public class Main {
  // Generic utility method: swap two indices of an array of any reference type
  static <T> void swap(T[] arr, int i, int j) {
    T tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;
  }

  public static void main(String[] args) {
    String[] names = { "Ada", "Bob", "Cyd" };
    swap(names, 0, 2); // works with String[]
    for (int k = 0; k < names.length; k = k + 1) {
      System.out.print(names[k] + (k < names.length - 1 ? " " : "\n"));
    }
  }
}

Code: static generic factory

Statik generic factory: of(T v) çağrısında tür çıkarımı yapılır. Kullanım temiz ve okunaklıdır.

public class Main {
  static class Box<T> {
    private final T value;
    private Box(T v) { this.value = v; }
    public T get() { return value; }
    // Static generic factory method inside a generic class
    public static <T> Box<T> of(T v) { return new Box<T>(v); }
  }

  public static void main(String[] args) {
    // Type inferred by diamond and method generic parameter
    Box<Integer> bi = Box.of(42);
    Box<String> bs = Box.of("hi");
    System.out.println(bi.get() + 1);
    System.out.println(bs.get().toUpperCase());
  }
}

Generic methods check

Quick check: How do you declare a generic method correctly?

Recap

Recap: Generic sınıflar (Pair<T,U>) tekrarı azaltır; generic metotlar (<T> imzası) sınıftan bağımsız çalışır. Tür çıkarımı çağrıları sadeleştirir.

Frequently asked questions

Is the “Generic Methods/Classes” lesson free?

Yes — the full text of “Generic Methods/Classes” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Generic Methods/Classes”?

Define generic classes with type parameters and write generic methods with <T> before the return type. Learn where type inference helps. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Generic Methods/Classes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Interfaces in Practice
  2. Generics Basics (T, inference)
  3. Generic Methods/Classes
← Back to Java Academy