1
public class PairT <T>{
    public T first;
    public T second;

    public T getFirst() {
        return first;
    }

    public void setFirst(T first) {
        this.first = first;
    }

    public T getSecond() {
        return second;
    }

    public void setSecond(T second) {
        this.second = second;
    }

    public PairT(T first, T second) {
        this.first = first;
        this.second = second;
    }
    public PairT() {
    }

What is the diffirence between these two makePair methods essentially? Why the second one is grammatically illegal?

First one:

public static <U> PairT<U> makePair(Class<U> cl) throws Exception {
        return new PairT<U>(cl.getConstructor().newInstance(),
                            cl.getConstructor().newInstance());
    }

Second:

public static <U> PairT<U> makePair(U cl) throws Exception {
        return new PairT<U(
                  cl.getClass().getConstructor().newInstance(), 
                  cl.getClass().getConstructor().newInstance());
    }
4
  • 7
    first essential difference: first is posted as text (easy to read, copy, ...); second is pasted as image (hard to read, impossible to copy, ...) || Why should I not upload images of code/data/errors when asking a question? (at least not only as image) Commented Jul 15, 2022 at 15:13
  • 6
    It's not grammatically illegal. Your grammar is fine. It's the type system that's complaining. U is erased to Object. cl.getClass() is Class<?>. The result of creating a class with that constructor is an instance of unknown type. Commented Jul 15, 2022 at 15:16
  • @user16320675 sorry, I just want to show the error message on the image Commented Jul 16, 2022 at 6:06
  • @Michael For the first case, what does param Class<U> look like after type erase? It becomes raw type Class ? Commented Jul 16, 2022 at 6:28

1 Answer 1

1

Object.getClass() returns Class<?>, so you need to cast. makePair2 does so directly but at the end, makePair3 uses getClass which isolates where the casting is required.

    public static <U> PairT<U> makePair1(Class<U> cl) throws Exception {
        return new PairT<U>(cl.getConstructor().newInstance(),
                cl.getConstructor().newInstance());
    }

    public static <U> PairT<U> makePair2(U cl) throws Exception {
        return new PairT<U>(
                (U) cl.getClass().getConstructor().newInstance(),
                (U) cl.getClass().getConstructor().newInstance());
    }

    private static <U> Class<U> getClass(U u) {
        return (Class<U>) u.getClass();
    }

    public static <U> PairT<U> makePair3(U cl) throws Exception {
        return new PairT<U>(
                getClass(cl).getConstructor().newInstance(),
                getClass(cl).getConstructor().newInstance());
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.