What is the syntax for annotation type elements?












6














How do I read/understand the following statement in Java?



Class<?> groups() default {};

Class < ? extends Payload> payload() default {};


I think I can understand them individually but I don't know if I get good sense of what it means in its entirety.



Individually:





  1. Class<?> means any class and Class< ? extends Payload> means any class that extends the Payload class


  2. [ ] seems to refer to any array of classes. Is that correct?


  3. groups() and payload() are method names.


  4. default{} Use this when there is no implementation?


I am really not sure how to understand the above statements? Any help would be very much appreciated.










share|improve this question
























  • This code is from an annotation, isn't?
    – user10639668
    Nov 22 at 18:14










  • Yes..I came across it when working on developing a validator. This is used in the validator annotation interface
    – Aaron
    Nov 22 at 18:16






  • 2




    Please familiarize yourself with StackOverflows editor. Using code formatting and syntax highlighting would greatly help boosting the quality of your question :)
    – Zabuza
    Nov 22 at 18:17
















6














How do I read/understand the following statement in Java?



Class<?> groups() default {};

Class < ? extends Payload> payload() default {};


I think I can understand them individually but I don't know if I get good sense of what it means in its entirety.



Individually:





  1. Class<?> means any class and Class< ? extends Payload> means any class that extends the Payload class


  2. [ ] seems to refer to any array of classes. Is that correct?


  3. groups() and payload() are method names.


  4. default{} Use this when there is no implementation?


I am really not sure how to understand the above statements? Any help would be very much appreciated.










share|improve this question
























  • This code is from an annotation, isn't?
    – user10639668
    Nov 22 at 18:14










  • Yes..I came across it when working on developing a validator. This is used in the validator annotation interface
    – Aaron
    Nov 22 at 18:16






  • 2




    Please familiarize yourself with StackOverflows editor. Using code formatting and syntax highlighting would greatly help boosting the quality of your question :)
    – Zabuza
    Nov 22 at 18:17














6












6








6







How do I read/understand the following statement in Java?



Class<?> groups() default {};

Class < ? extends Payload> payload() default {};


I think I can understand them individually but I don't know if I get good sense of what it means in its entirety.



Individually:





  1. Class<?> means any class and Class< ? extends Payload> means any class that extends the Payload class


  2. [ ] seems to refer to any array of classes. Is that correct?


  3. groups() and payload() are method names.


  4. default{} Use this when there is no implementation?


I am really not sure how to understand the above statements? Any help would be very much appreciated.










share|improve this question















How do I read/understand the following statement in Java?



Class<?> groups() default {};

Class < ? extends Payload> payload() default {};


I think I can understand them individually but I don't know if I get good sense of what it means in its entirety.



Individually:





  1. Class<?> means any class and Class< ? extends Payload> means any class that extends the Payload class


  2. [ ] seems to refer to any array of classes. Is that correct?


  3. groups() and payload() are method names.


  4. default{} Use this when there is no implementation?


I am really not sure how to understand the above statements? Any help would be very much appreciated.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 18:37









Sotirios Delimanolis

207k39476568




207k39476568










asked Nov 22 at 18:09









Aaron

333




333












  • This code is from an annotation, isn't?
    – user10639668
    Nov 22 at 18:14










  • Yes..I came across it when working on developing a validator. This is used in the validator annotation interface
    – Aaron
    Nov 22 at 18:16






  • 2




    Please familiarize yourself with StackOverflows editor. Using code formatting and syntax highlighting would greatly help boosting the quality of your question :)
    – Zabuza
    Nov 22 at 18:17


















  • This code is from an annotation, isn't?
    – user10639668
    Nov 22 at 18:14










  • Yes..I came across it when working on developing a validator. This is used in the validator annotation interface
    – Aaron
    Nov 22 at 18:16






  • 2




    Please familiarize yourself with StackOverflows editor. Using code formatting and syntax highlighting would greatly help boosting the quality of your question :)
    – Zabuza
    Nov 22 at 18:17
















This code is from an annotation, isn't?
– user10639668
Nov 22 at 18:14




This code is from an annotation, isn't?
– user10639668
Nov 22 at 18:14












Yes..I came across it when working on developing a validator. This is used in the validator annotation interface
– Aaron
Nov 22 at 18:16




Yes..I came across it when working on developing a validator. This is used in the validator annotation interface
– Aaron
Nov 22 at 18:16




2




2




Please familiarize yourself with StackOverflows editor. Using code formatting and syntax highlighting would greatly help boosting the quality of your question :)
– Zabuza
Nov 22 at 18:17




Please familiarize yourself with StackOverflows editor. Using code formatting and syntax highlighting would greatly help boosting the quality of your question :)
– Zabuza
Nov 22 at 18:17












1 Answer
1






active

oldest

votes


















5















default {} --> Use this when there is no implementation?




In an Annotation definition you can specify a default value for a parameter. The {} after default is an array literal for an empty array. You could also set it to some non empty value. This also works for other objects too. eg the Data annotation from lombok where a default string is declared:



public @interface Data {
String staticConstructor() default "";
}



Class means any class and "Class< ? extends Payload>" means any class
that extends the Payload class




Yes




[ ] --> Seems to refer to any array of classes. Is that correct?




Yes




groups() and payload() are method names.




They are the name of the parameter used in annotation declaration, as well as the name of the getter methods for those values. eg you could define:



@Data(staticConstructor = "of") class Foobar {}


And later you could get the value by using the getter method created:



Data dataAnnotation = Foobar.class.getAnnotation(Data.class);
String staticConstructor = dataAnnotation.staticConstructor();


Note that you will not actually be able to retrieve the value for an annotation in runtime unless the annotation definition is also annotated with @Retention(RetentionPolicy.RUNTIME)






share|improve this answer



















  • 1




    Thank you so much. This helped with clarifying my thoughts.
    – Aaron
    Dec 5 at 18:16













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436286%2fwhat-is-the-syntax-for-annotation-type-elements%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5















default {} --> Use this when there is no implementation?




In an Annotation definition you can specify a default value for a parameter. The {} after default is an array literal for an empty array. You could also set it to some non empty value. This also works for other objects too. eg the Data annotation from lombok where a default string is declared:



public @interface Data {
String staticConstructor() default "";
}



Class means any class and "Class< ? extends Payload>" means any class
that extends the Payload class




Yes




[ ] --> Seems to refer to any array of classes. Is that correct?




Yes




groups() and payload() are method names.




They are the name of the parameter used in annotation declaration, as well as the name of the getter methods for those values. eg you could define:



@Data(staticConstructor = "of") class Foobar {}


And later you could get the value by using the getter method created:



Data dataAnnotation = Foobar.class.getAnnotation(Data.class);
String staticConstructor = dataAnnotation.staticConstructor();


Note that you will not actually be able to retrieve the value for an annotation in runtime unless the annotation definition is also annotated with @Retention(RetentionPolicy.RUNTIME)






share|improve this answer



















  • 1




    Thank you so much. This helped with clarifying my thoughts.
    – Aaron
    Dec 5 at 18:16


















5















default {} --> Use this when there is no implementation?




In an Annotation definition you can specify a default value for a parameter. The {} after default is an array literal for an empty array. You could also set it to some non empty value. This also works for other objects too. eg the Data annotation from lombok where a default string is declared:



public @interface Data {
String staticConstructor() default "";
}



Class means any class and "Class< ? extends Payload>" means any class
that extends the Payload class




Yes




[ ] --> Seems to refer to any array of classes. Is that correct?




Yes




groups() and payload() are method names.




They are the name of the parameter used in annotation declaration, as well as the name of the getter methods for those values. eg you could define:



@Data(staticConstructor = "of") class Foobar {}


And later you could get the value by using the getter method created:



Data dataAnnotation = Foobar.class.getAnnotation(Data.class);
String staticConstructor = dataAnnotation.staticConstructor();


Note that you will not actually be able to retrieve the value for an annotation in runtime unless the annotation definition is also annotated with @Retention(RetentionPolicy.RUNTIME)






share|improve this answer



















  • 1




    Thank you so much. This helped with clarifying my thoughts.
    – Aaron
    Dec 5 at 18:16
















5












5








5







default {} --> Use this when there is no implementation?




In an Annotation definition you can specify a default value for a parameter. The {} after default is an array literal for an empty array. You could also set it to some non empty value. This also works for other objects too. eg the Data annotation from lombok where a default string is declared:



public @interface Data {
String staticConstructor() default "";
}



Class means any class and "Class< ? extends Payload>" means any class
that extends the Payload class




Yes




[ ] --> Seems to refer to any array of classes. Is that correct?




Yes




groups() and payload() are method names.




They are the name of the parameter used in annotation declaration, as well as the name of the getter methods for those values. eg you could define:



@Data(staticConstructor = "of") class Foobar {}


And later you could get the value by using the getter method created:



Data dataAnnotation = Foobar.class.getAnnotation(Data.class);
String staticConstructor = dataAnnotation.staticConstructor();


Note that you will not actually be able to retrieve the value for an annotation in runtime unless the annotation definition is also annotated with @Retention(RetentionPolicy.RUNTIME)






share|improve this answer















default {} --> Use this when there is no implementation?




In an Annotation definition you can specify a default value for a parameter. The {} after default is an array literal for an empty array. You could also set it to some non empty value. This also works for other objects too. eg the Data annotation from lombok where a default string is declared:



public @interface Data {
String staticConstructor() default "";
}



Class means any class and "Class< ? extends Payload>" means any class
that extends the Payload class




Yes




[ ] --> Seems to refer to any array of classes. Is that correct?




Yes




groups() and payload() are method names.




They are the name of the parameter used in annotation declaration, as well as the name of the getter methods for those values. eg you could define:



@Data(staticConstructor = "of") class Foobar {}


And later you could get the value by using the getter method created:



Data dataAnnotation = Foobar.class.getAnnotation(Data.class);
String staticConstructor = dataAnnotation.staticConstructor();


Note that you will not actually be able to retrieve the value for an annotation in runtime unless the annotation definition is also annotated with @Retention(RetentionPolicy.RUNTIME)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 18:44

























answered Nov 22 at 18:27









flakes

6,51611850




6,51611850








  • 1




    Thank you so much. This helped with clarifying my thoughts.
    – Aaron
    Dec 5 at 18:16
















  • 1




    Thank you so much. This helped with clarifying my thoughts.
    – Aaron
    Dec 5 at 18:16










1




1




Thank you so much. This helped with clarifying my thoughts.
– Aaron
Dec 5 at 18:16






Thank you so much. This helped with clarifying my thoughts.
– Aaron
Dec 5 at 18:16




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436286%2fwhat-is-the-syntax-for-annotation-type-elements%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo