About Question enthuware.ocpjp.v8.2.1824 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
d0wnvk@gmail.com
Posts: 7
Joined: Wed Jun 06, 2018 6:46 am
Contact:

About Question enthuware.ocpjp.v8.2.1824 :

Post 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)

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

saurabh.agarwal560
Posts: 11
Joined: Thu May 28, 2020 9:53 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

saurabh.agarwal560
Posts: 11
Joined: Thu May 28, 2020 9:53 am
Contact:

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

Post by saurabh.agarwal560 »

Ok got it. Thanks for Clarification.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 48 guests