18
<T extends Drawable & DrawerToggle> ActionBarDrawerToggle(
        Activity activity, 
        Toolbar toolbar,
        DrawerLayout drawerLayout, 
        T slider,
        @StringRes int openDrawerContentDescRes,
        @StringRes int closeDrawerContentDescRes) {

During browsing source code of the class ActionBarDrawerToggle.java, I've found this constructor is declared without access specifier. Rather, its declaration starts with

<T extends Drawable & DrawerToggle>

Please explain, what does it really mean?

senshin
  • 10,022
  • 7
  • 46
  • 59
Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
  • where do you see that code? can we see it somewhere online? – Stultuske Feb 05 '16 at 11:17
  • 4
    "without access specifier" specifies that it has package-private visibility. In other words, unless you're developing code in the same package, you don't really need to know this exists. – Andy Turner Feb 05 '16 at 11:19
  • 1
    It is a method type parameter. This might help you. https://docs.oracle.com/javase/tutorial/java/generics/boundedTypeParams.html It is not related to whether the constructor is declared `public` or not (but instead, as @AndyTurner mentioned, package-private, which is a kind of "default" access modifier as well). – SOFe Feb 05 '16 at 11:20
  • @Stultuske You can just `Ctrl+Click` and browse in Android Studio once you downloaded the source code, but I have edited and linked source url as well. – Vikas Patidar Feb 05 '16 at 11:22
  • 2
    @VikasPatidar : Please avoid pasting images for code, rather simply paste the code itself, it's easier to copy/edit/... – Zbynek Vyskovsky - kvr000 Feb 05 '16 at 11:38
  • 1
    the summary of what i understood from below answers is **** acts like a reference for all parameters that use **T** . so you can have 10 parameters of T without specifying ** 10 times – Srinath Ganesh Feb 05 '16 at 12:36
  • Look [here](http://www.java2s.com/Tutorials/Java/Java_Object_Oriented_Design/0370__Java_Generic_Methods_Constructors.htm) and search for "Generic Constructor" – Matteo Umili Feb 05 '16 at 14:36

3 Answers3

23

T is generic type declaration which is then used as a type for one of the arguments - slider.

<T extends Drawable & DrawerToggle > specifically means that T must extend/implement both Drawable and DrawerToggle classes/interfaces.

The access specifier is not mandatory. In case it's missing it means the class/method is accessible only from classes within the same package.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
8

Like normal methods, constructors can take type parameters. This is mentioned in this section of the Java Language Specification:

https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.8

From the syntax mentioned there, you can have type parameters in the signature:

TypeParameters:

< TypeParameterList >

TypeParameterList:

TypeParameter {, TypeParameter}

and where TypeParameter is specified as

TypeParameter:

{TypeParameterModifier} Identifier [TypeBound]

TypeParameterModifier:

Annotation

TypeBound:

extends TypeVariable

extends ClassOrInterfaceType {AdditionalBound}

AdditionalBound:

& InterfaceType

M A
  • 71,713
  • 13
  • 134
  • 174
3

This block specifies slider parameters type (value of slider parameter must extends drawable and drawabletoggle classes (or interfaces)). Search generics for more info.

Hyperion
  • 382
  • 8
  • 16