Compiler error with calling Generic methods java

Jeena

I have created a generic class with few methods. I just want to call the method and add value but its not working. Here is the code

public interface GenericInterface<T> {
    public T getValue();
    public  void setValue(T t);
}

public class FirstGeneric<T> implements GenericInterface<T> {
    private T value;

    public FirstGeneric(T _value) {
        this.value = _value;
    }

    @Override
    public T getValue() {
        return value;
    }

    @Override
    public void setValue(T t) {
        this.value = t;
    }

    @SuppressWarnings("unchecked")
    public static <T> T addValues(FirstGeneric<T> myGeneric, FirstGeneric<T> myGeneric1) {
        T result = null;
        if (myGeneric != null && myGeneric1 != null) {
            T t1 = myGeneric.getValue();
            T t2 = myGeneric1.getValue();
            result = (T) t1.toString().concat(t2.toString());
            System.out.println("Result is:=" + result);
        }
        return result;
    }

    public static void main(String args[]) {
        Integer int1 = 1;
        FirstGeneric<Integer> myInt = new FirstGeneric<Integer>(int1);
        String value = "Hello";
        FirstGeneric<String> myString = new FirstGeneric<String>(value);
        addValues(myString,  myInt);
    }
}

But i am getting compilation error at last line.

The method addValues(FirstGeneric, FirstGeneric) in the type FirstGeneric is not applicable for the arguments (FirstGeneric, FirstGeneric)

What I am doing wrong? Thanks.

Eran

addValues has a single generic type parameter, which means you can't pass to it both a FirstGeneric<Integer> and a FirstGeneric<String>.

To make the addValues(myString, myInt) call valid, you can define two generic type parameters in your method:

public static <T,U> String addValues(FirstGeneric<T> myGeneric, FirstGeneric<U> 
   myGeneric1) {
    String result = null;
    if (myGeneric != null && myGeneric1 != null) {
        T t1 = myGeneric.getValue();
        U t2 = myGeneric1.getValue();
        result = t1.toString().concat(t2.toString());
        System.out.println("Result is:=" + result);
    }
    return result;
}

Note that I changed the return type to String, since it's clearly a String (produced by t1.toString().concat(t2.toString())), so you can't cast it to T and hope that T would be a String.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Compiler error on Java generic interface with a List<> method

From Java

Calling static generic methods

From Java

Multiple wildcards on a generic methods makes Java compiler (and me!) very confused

From Java

Java Generics - calling specific methods from generic-typed ones

From Dev

Syntax shortcut for calling generic methods

From Java

Generic Binary Search Tree Implementation in Java - calling generic methods, and cast exceptions

From Dev

C# compiler fails with misleading error when calling generic extension method with a dynamic argument

From Dev

RSPEC Calling Methods Error

From Dev

Generic error: Runtime compiler is not loaded,

From Java

Getting compile error while implementing a simple Generic methods in java

From Java

Java Overriding Generic Methods

From Java

Java: Generic methods and numbers

From Java

Invoking Java Generic Methods

From Dev

XPages - Calling Java methods

From Dev

Calling the methods in java

From Dev

Calling Java Methods in XSLT

From Java

Calling multiple methods in Java

From Java

Calling the methods in a class in Java

From Dev

Calling methods on objects Java

From Dev

How is generic information used by the compiler if generic static methods are called?

From Dev

Generic type parameters when calling non-generic trait methods

From Java

Java - generic classes hierarchy and generic methods overloading

From Dev

Java overloading methods with generic and a collection of a generic

From Java

Java generic methods in generics classes

From Java

Return Type of Java Generic Methods

From Java

Generic Methods and Type Inferencing in Java

From Java

Calling methods on string literals (Java)

From Java

Calling methods of "parent" component in Java

From Java

Calling two Java methods asynchronously

Related Related

  1. 1

    Compiler error on Java generic interface with a List<> method

  2. 2

    Calling static generic methods

  3. 3

    Multiple wildcards on a generic methods makes Java compiler (and me!) very confused

  4. 4

    Java Generics - calling specific methods from generic-typed ones

  5. 5

    Syntax shortcut for calling generic methods

  6. 6

    Generic Binary Search Tree Implementation in Java - calling generic methods, and cast exceptions

  7. 7

    C# compiler fails with misleading error when calling generic extension method with a dynamic argument

  8. 8

    RSPEC Calling Methods Error

  9. 9

    Generic error: Runtime compiler is not loaded,

  10. 10

    Getting compile error while implementing a simple Generic methods in java

  11. 11

    Java Overriding Generic Methods

  12. 12

    Java: Generic methods and numbers

  13. 13

    Invoking Java Generic Methods

  14. 14

    XPages - Calling Java methods

  15. 15

    Calling the methods in java

  16. 16

    Calling Java Methods in XSLT

  17. 17

    Calling multiple methods in Java

  18. 18

    Calling the methods in a class in Java

  19. 19

    Calling methods on objects Java

  20. 20

    How is generic information used by the compiler if generic static methods are called?

  21. 21

    Generic type parameters when calling non-generic trait methods

  22. 22

    Java - generic classes hierarchy and generic methods overloading

  23. 23

    Java overloading methods with generic and a collection of a generic

  24. 24

    Java generic methods in generics classes

  25. 25

    Return Type of Java Generic Methods

  26. 26

    Generic Methods and Type Inferencing in Java

  27. 27

    Calling methods on string literals (Java)

  28. 28

    Calling methods of "parent" component in Java

  29. 29

    Calling two Java methods asynchronously

HotTag

Archive