JAVA

instance methods & static methods in generic methods

neal89 2025. 3. 7. 06:29

1️⃣ What is a Generic Method?

  • A generic method allows dynamic type specification at the method level, making it more flexible.
  • Both instance methods and static methods can use generics.
public class Utility {
    // 🔹 Static Generic Method
    public static <T> T identity(T value) {
        return value;
    }

    // 🔹 Instance Generic Method
    public <T> T instanceIdentity(T value) {
        return value;
    }
}

2️⃣ Difference

Static Generic Method (static <T>) Instance Generic Method (<T>)

Object Required? Can be called directly using the class name Requires an instance (object) to be called
Scope of Type Parameter The generic type applies only to the method The generic type is independent of the class’s type parameter
Relation to Object State Independent of object state Can be related to object state
Example Usage Utility.identity(10) or Utility.<Integer>identity(10) new Utility().instanceIdentity(10)

3️⃣ Static Generic Method (static <T>)

Does not require an object; can be called directly using the class name
Commonly used in utility methods

public class Utility {
    // 🔹 Static Generic Method
    public static <T> T identity(T value) {
        return value;
    }
}

public class Main {
    public static void main(String[] args) {
        String result = Utility.identity("Hello");  // ✅ Automatic type inference
        Integer num = Utility.<Integer>identity(100); // ✅ Explicit type specification

        System.out.println(result); // Hello
        System.out.println(num);    // 100
    }
}

Called using the class name (Utility.identity())
T is determined at the time of method invocation
Commonly used in utility methods like Collections.sort()


4️⃣ Instance Generic Method (<T>)

Requires an object to be instantiated before calling
Can be associated with the object’s state

public class Utility {
    // 🔹 Instance Generic Method
    public <T> T instanceIdentity(T value) {
        return value;
    }
}

public class Main {
    public static void main(String[] args) {
        Utility util = new Utility(); // ✅ Requires an instance
        String result = util.instanceIdentity("Hello"); 
        Integer num = util.instanceIdentity(100);

        System.out.println(result); // Hello
        System.out.println(num);    // 100
    }
}

Requires object instantiation (new Utility().instanceIdentity())
T is determined at the time of method invocation
Can be used for operations related to the object's state


5️⃣ Why Can’t Static Methods Use Class’s Generic Type?

Static methods cannot access the class’s generic type <T> because they belong to the class, not an instance.

public class Box<T> {
    private T value;

    // ❌ Compilation Error: Cannot use 'T' in a static method
    public static void staticMethod(T value) {
        System.out.println(value);
    }
}

🚨 Error: Static method cannot use non-static type parameter 'T'
Static methods do not belong to an instance, so they cannot access the class’s type parameter.
➡ The solution is to declare a separate type parameter within the method:

public class Box<T> {
    private T value;

    // ✅ Correct way to define a static generic method
    public static <U> void staticMethod(U value) {
        System.out.println(value);
    }
}

Uses <U> instead of <T>, since <T> belongs to the instance level

 

'JAVA' 카테고리의 다른 글

daemon thread  (0) 2025.03.07
What happens if you call run() instead of thread.start()?  (0) 2025.03.07
Generic  (0) 2025.03.07
try-with-resources vs try-catch-finally  (0) 2025.03.05
중첩 클래스(Nested Class)  (0) 2025.03.03