Page 1 of 1

About Question enthuware.ocpjp.v8.2.1824 :

Posted: Mon Nov 12, 2018 1:06 am
by d0wnvk@gmail.com

Code: Select all

 public static <X> PlaceHolder<X, X> getDuplicateHolder(X x){
    return new PlaceHolder<X, X>(x, x); 
}
What does this single <X> in

Code: Select all

static <X> 
mean ?

Thanks)

Re: About Question enthuware.ocpjp.v8.2.1824 :

Posted: Mon Nov 12, 2018 1:45 am
by admin
<X> is the genreic type declaration. It tells the compiler that this method uses X as the name of generic type. So, when you try to actually call this method with, for example, String, X will be substituted with String.
See this for more detail: https://docs.oracle.com/javase/tutorial ... thods.html

Re: About Question enthuware.ocpjp.v8.2.1824 :

Posted: Thu May 28, 2020 11:31 am
by saurabh.agarwal560
According to given program Constructor accept different generic(K,V) type than getDuplicateHolder(X). Than how come if we call the constructor of PlaceHolder with (X,X) will compile successfully. In my point of view there should be error on line 4. Please clarify.

Re: About Question enthuware.ocpjp.v8.2.1824 :

Posted: Thu May 28, 2020 12:37 pm
by admin
K and V are just the names given to the types of the two parameters. They are not actual parameter types. You can certainly choose to pass same types. By using two different names, you are allowing the class to use two different types. In other words, the type of the second parameter doesn't have to be the same as the type of the first. But it can be if you want.

Re: About Question enthuware.ocpjp.v8.2.1824 :

Posted: Sat May 30, 2020 1:31 pm
by saurabh.agarwal560
Ok got it. Thanks for Clarification.