1
<D extends com.j256.ormlite.dao.Dao<T,?>,T> D getDao(Class<T> clazz)

I am not able to understand above statement

getDao(Class clazz) returns D

D having following constraints

D extends com.j256.ormlite.dao.Dao<T,?>

and extra ,T i am not able to understand.

Could you please explain it ?

1
  • This method requires two type parameters, D and T, and D has an additional extends constraint, depending on T. Don't get confused by this <T,?>,T, those do not belong together. Commented Aug 29, 2014 at 12:42

2 Answers 2

2

This method has two type parameters, D and T, with D having an additional extends constraint, depending on T. Don't get confused by this <T,?>,T syntax; the ,T does not belong to the constraint, but is the second parameter, telling Java that T is not the name of a concrete class.

If you add a space or swap the parameters, it will be clearer. Here's a similar, but somewhat simpler example. These method signatures are all equivalent:

<D extends List<T>,T>   D createListOf(Class<T> clazz)  // your version
<D extends List<T>, T>  D createListOf(Class<T> clazz)  // extra space
<T, D extends List<T>>  D createListOf(Class<T> clazz)  // different order

Keep in mind that, even though it may seem apparent that T is another type parameter, this is not clear to Java. There could be an actual class named T, so we have to be explicit that T is a type parameter.

class T { ... } // this T is not what I want!

Conversely, type parameters are not restricted to single characters. You could also have a type parameter called Foo, or even String, if you want to utterly confuse your co-workers. Maybe that makes clear why the declaration of all type parameters using <...> is necessary.

// just a deterrent example; don't do this! String as parameter, not class
<String, Foo extends List<String>> Foo createListOf(Class<String> clazz)
Sign up to request clarification or add additional context in comments.

3 Comments

\@tobias_kas you are saying seconds parameter of return type is too just tell Java that T is not the name of a concrete class. is it same information can also convey with first parameter itself ?
@AmitYadav You mean, shouldn't it be enough that T is used in the constraint for D? No. There's no rule that a class name must have more than one letter, thus T could as well the the name of a concrete class.
\@tobias_k I agree with you that we can create a class of name T. I think I don't have enough knowledge on Generic type. All you mean to say that in return type T as 2nd parameter is to inform java that T is not a concrete class...hope later i'll under stand this concept. thanks!
1

This method will:

  • Return an object of type D
  • Where D is or extends com.j256.ormlite.dao.Dao, parametrized with an object of type T or extending/implementing T and an unknown type parameter
  • If given as argument a class of type T

It uses a lot of generic abstraction, which is not surprising given it delivers a DAO (Data Access Object).

3 Comments

I am confusing about return type. It returns D <1st constraints, 2nd constraints> 1st constraints = D extends com.j256.ormlite.dao.Dao<T,?> it fine for me...I am asking is it 2nd constraints really needed ?
I think I see what you mean. Since it's a generic method, the type parameter T must be declared there. Otherwise it could not be referenced by the method parameter, nor by the parametrization of com.j256.ormlite.dao.Dao.
\@Mena I am not confused with method arguments T instead return type T mentioned as a second parameter <1st parameter, T> D

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.