-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathannotation-java.html
More file actions
813 lines (710 loc) · 37.8 KB
/
annotation-java.html
File metadata and controls
813 lines (710 loc) · 37.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="description" content="Java Annotations allow us to add metadata information into our source code, although they are not a part of the program itself. Annotations were added to the java from JDK 5. Annotation has no direct effect on the operation of the code they annotate (i.e. it does not affect the execution of the program).An annotation always starts with the symbol @ followed by the annotation name. The symbol @ indicates to the compiler that this is an annotation."/>
<meta name="keywords" content="Java Annotations,Annotations in Java,what is Annotations in java?,about Annotations, Annotations,C, C++, Python, Java, HTML, CSS, JavaScript, SQL, PHP, jQuery, XML, DOM, Bootstrap, Tutorials, Articles, Programming, training, learning, quiz, preferences, examples, code"/>
<title>Exceptions in Java | Java Tutorials | CodeMistic</title>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-MCNC43J');</script>
<!-- End Google Tag Manager -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YPTQSLH0XW"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-YPTQSLH0XW');
</script>
<script data-ad-client="ca-pub-5352867889640158" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Favicon -->
<link rel="icon" type="img/png" href="../favicon.png"/>
<!-- FONT AWESOME -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.1/css/all.css"
integrity="sha384-xxzQGERXS00kBmZW/6qxqJPyxW3UR0BPsL4c8ILaIWXva5kFi7TxkIIaMiKtqV1Q" crossorigin="anonymous"/>
<!-- FONTS -->
<link href="https://fonts.googleapis.com/css2?family=Kalam:wght@700&family=Ubuntu&family=Varela+Round&display=swap"
rel="stylesheet"/>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity=""
crossorigin="anonymous"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity=""
crossorigin="anonymous"/>
<!-- CSS -->
<link rel="stylesheet" href="../lang_styles.css"/>
</head>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-MCNC43J"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<!-- NAVIGATION BAR -->
<nav class="navbar fixed-top navbar-expand-xl navbar-dark">
<a class="navbar-brand" href="../index.html">CODEMISTIC</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="../index.html">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Tutorials
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="../cpp.html">C++</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="../python.html">Python</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="../java.html">Java</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="../dsa.html">Data Structure and Algorithms</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="../tutorials.html">All Tutorials</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="../events.html">Events</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../teen-code.html">Teen-Code</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../campus.html">Campus Programme</a>
</li>
<!-- <li class="nav-item">
<a class="nav-link" href="https://docs.google.com/forms/d/e/1FAIpQLSc2O28YJpxQc6kh2tKpI_Lf5MwfwGXVnhMs0g5EEXobJ4Beeg/viewform?usp=sf_link" target="_blank">Contribute</a>
</li> -->
<li class="nav-item">
<a class="nav-link" href="../register.html">Join Our Community</a>
</li>
</ul>
</div>
</nav>
<!-- Accordion -->
<section class="lbar">
<div class="lang-img col-md-2">
<img src="../logo/java.png" alt="dsa" class="cpp-img">
</div>
<ul id="accordion" class="accordion ml-5">
<li>
<div class="link">Java Introduction<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse ">
<li>
<a href=" hello-world-java.html" class="closed" >Java Hello World</a>
</li>
<li>
<a href="JVM-JRE-JDK-java.html" class="closed">Java JVM, JRE and JDK</a>
</li>
<li>
<a href="data-types-java.html" class="closed">Java Data Types</a>
</li>
<li>
<a href="operators-java.html" class="closed">Java Operators</a>
</li>
<li>
<a href="input-and-output-java.html" class="closed">Java Input and Output</a>
</li>
<li>
<a href="expressions-and-blocks-java.html" class="closed">Java Expressions & Blocks</a>
</li>
<li>
<a href="comment-java.html" class="closed">Java Comment</a>
</li>
</ul>
</li>
<li>
<div class="link">Java Flow Control<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse ">
<li><a href="if-else-java.html" class="closed">Java if...else</a></li>
<li><a href="switch-statement-java.html" class="closed">Java switch Statement</a></li>
<li><a href="for-loop-java.html" class="closed">Java for Loop</a></li>
<li><a href="for-else-loop-java.html" class="closed">Java for-else Loop</a></li>
<li><a href="while-loop-java.html" class="closed">Java while Loop</a></li>
<li><a href="break-statement-java.html" class="closed">Java break Statement</a></li>
<li><a href="continue-statement-java.html" class="closed">Java continue Statement</a></li>
</ul>
</li>
<li>
<div class="link">Java Arrays<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse">
<li><a href="arrays-java.html" class="closed" >Java Arrays</a></li>
<li><a href="multidimensional-array-java.html" class="closed">Multidimensional Array</a></li>
<li><a href="copy-arrays-java.html" class="closed" >Java Copy Arrays</a></li>
</ul>
</li>
<li>
<div class="link">Java OOP(I)<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse ">
<li><a href="class-objects-java.html" class="closed"> Java Class and Objects</a></li>
<li><a href="methods-java.html" class="closed">Java Methods</a></li>
<li><a href="constructor-java.html" class="closed"> Java Constructor</a></li>
<li><a href="string-java.html" class="closed"> Java String</a></li>
<li><a href="access-modifiers-java.html" class="closed"> Java Access Modifiers</a></li>
<li><a href="this-keyword-java.html" class="closed"> Java this Keyword</a></li>
<li><a href="final-keyword-java.html" class="closed"> Java final Keyword</a></li>
<li><a href="recursion-java.html" class="closed"> Java Recursion</a></li>
<li><a href="instance-of-operator-java.html" class="closed"> Java instanceOf Operator</a></li>
</ul>
</li>
<li>
<div class="link">Java OOP(II)<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse ">
<li><a href="inheritance-java.html" class="closed">Java Inheritance</a></li>
<li><a href="methods-overriding-java.html" class="closed">Java Methods Overriding </a></li>
<li><a href="super-keyword-java.html" class="closed">Java Super Keyword</a></li>
<li><a href="abstract-class-method-java.html" class="closed">Abstract Class & Method</a></li>
<li><a href="interfaces-java.html" class="closed">Java Interfaces</a></li>
<li><a href="polymorphism-java.html" class="closed">Java Polymorphism</a></li>
<li><a href="encapsulation-java.html" class="closed">Java Encapsulation</a></li>
</ul>
</li>
<li>
<div class="link">Java OOP(III)<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse">
<li><a href="nested-inner-class-java.html" class="closed">Nested & Inner Class</a></li>
<li><a href="static-class-java.html" class="closed">Java Static class </a></li>
<li><a href="anonymous-java.html" class="closed">Java Anonymous</a></li>
<li><a href="singleton-java.html" class="closed">Java Singleton</a></li>
<li><a href="enum-class-java.html" class="closed">Java enum Class </a></li>
<li><a href="enum-constructor-java.html" class="closed">Java enum Constructor</a></li>
<li><a href="enum-string-java.html" class="closed">Java enum String</a></li>
<li><a href="reflection-java.html" class="closed">Java Reflection</a></li>
</ul>
</li>
<li>
<div class="link">Java Exception Handling<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse show">
<li><a href="exceptions-java.html" class="closed" >Java Exceptions</a>
</li>
<li><a href="exception-handling-java.html" class="closed" >Java Exception Handling</a>
</li>
<li> <a href="throw-throws-java.html" class="closed" >Java throw and throws</a>
</li>
<li><a href="catch-multiple-exceptions-java.html" class="closed" >Java catch Multiple Exceptions</a>
</li>
<li ><a href="try-with-resources-java.html" class="closed" >Java try-with-resources</a>
</li>
<li ><a href="annotation-java.html" class="opened" >Java Annotation</a>
</li>
<li><a href="logging-java.html" class="closed" >Java Logging</a>
</li>
<li><a href="assertions-java.html" class="closed" >Java Assertions</a>
</li>
</ul>
</li>
<li>
<div class="link">Java List<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse">
<li><a href="collections-framework-java.html" class="closed" >Java Collections Framework</a></li>
<li><a href="collection-interface-java.html" class="closed" >Java Collection Interface</a></li>
<li><a href="list-interface-java.html" class="closed" >Java List Interface</a></li>
<li><a href="array-list-java.html" class="closed" >Java ArrayList</a ></li>
<li> <a href="vector-java.html" class="closed" >Java Vector</a></li>
<li> <a href="stack-java.html" class="closed" >Java Stack</a></li>
</ul>
</li>
<li>
<div class="link">Java Queue<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse">
<li> <a href="queue-interface-java.html" class="closed">Java Queue Interface </a></li>
<li> <a href="priority-queue-java.html" class="closed">Java PriorityQueue </a></li>
<li> <a href="deque-interface-java.html" class="closed">Java Deque Interface </a></li>
<li> <a href="linked-list-java.html" class="closed">Java LinkedList </a></li>
<li> <a href="array-deque-java.html" class="closed">Java ArrayDeque </a></li>
<li> <a href="blocking-queue-interface-java.html"class="closed">Java BlockingQueue Interface </a></li>
<li> <a href="array-blocking-queue-java.html" class="closed">Java ArrayBlockingQueue </a></li>
<li> <a href="linked-blocking-queue-java.html" class="closed">Java LinkedBlockingQueue </a></li>
</ul>
</li>
<li>
<div class="link">Java Map<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse">
<li><a href="map-interface-java.html" class="closed" >Java Map Interface</a> </li>
<li><a href="hashmap-java.html" class="closed" >Java HashMap</a> <li>
<li><a href="linked-hashmap-java.html" class="closed" >Java LinkedHashMap</a> <li>
<li><a href="weak-hashmap-java.html" class="closed" >Java WeakHashMap</a> <li>
<li><a href="enum-map-java.html" class="closed" >Java EnumMap</a> <li>
<li><a href="sorted-map-interface-java.html" class="closed" >Java SortedMap Interface</a> <li>
<li><a href="navigable-map-interface-java.html" class="closed" >Java NavigableMap Interface</a> <li>
<li><a href="tree-map-java.html" class="closed" >Java TreeMap</a> <li>
<li><a href="concurrent-map-interface-java.html" class="closed" >Java ConcurrentMap Interface</a> <li>
<li><a href="concurrent-hashmap-java.html" class="closed" >Java ConcurrentHashMap</a> <li>
</ul>
</li>
<li>
<div class="link">Java Set<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse">
<li > <a href="set-interface-java.html" class="closed">Java Set Interface</a> </li>
<li><a href="hashset-java.html" class="closed">Java HashSet</a></li>
<li><a href="linked-hashset-java.html" class="closed">Java LinkedHashSet</a> </li>
<li><a href="enum-set-java.html" class="closed">Java EnumSet</a></li>
<li><a href="sorted-set-interface-java.html" class="closed">Java SortedSet Interface</a></li>
<li><a href="navigable-set-interface-java.html" class="closed">Java NavigableSet Interface</a></li>
<li><a href="tree-set-java.html" class="closed">Java TreeSet</a></li>
<li><a href="algorithms-java.html" class="closed">Java Algorithms</a></li>
<li><a href="iterator-java.html" class="closed">Java Iterator</a></li>
<li><a href="list-iterator-java.html" class="closed">Java ListIterator</a></li>
</ul>
</li>
<li>
<div class="link">Java I/O Streams<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse">
<li > <a href="I/O-streams-java.html" class="closed">Java I/O Streams</a> </li>
<li><a href="input-stream-java.html" class="closed">Java Input Stream</a></li>
<li><a href="output-stream-java.html" class="closed">Java Output Stream</a> </li>
<li><a href="file-input-stream-java.html" class="closed">Java FileInput Stream</a></li>
<li><a href="file-output-stream-java.html" class="closed">Java FileOutput Stream</a></li>
<li><a href="bytearray-input-stream-java.html" class="closed">Java ByteArrayInput Stream</a></li>
<li><a href="bytearray-output-stream-java.html" class="closed">Java ByteArrayOutput Stream</a></li>
<li><a href="object-input-stream-java.html" class="closed">Java ObjectInput Stream</a></li>
<li><a href="object-output-stream-java.html" class="closed">Java ObjectOutput Stream</a></li>
<li><a href="buffered-input-java.html" class="closed">Java BufferedInput</a></li>
<li><a href="buffered-output-java.html" class="closed">Java BufferedOutput</a></li>
<li><a href="print-input-java.html" class="closed">Java PrintInput</a></li>
</ul>
</li>
<li>
<div class="link">Java Reader/Writer<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse">
<li > <a href="reader-java.html" class="closed">Java Reader</a> </li>
<li><a href="writer-java.html" class="closed">Java Writer</a></li>
<li><a href="input-stream-reader-java.html" class="closed">Java InputStreamReader</a> </li>
<li><a href="output-stream-writer-java.html" class="closed">Java OutputStreamWriter</a></li>
<li><a href="file-reader-java.html" class="closed">Java FileReader</a></li>
<li><a href="file-writer-java.html" class="closed">Java FileWriter</a></li>
<li><a href="buffered-reader-java.html" class="closed">Java BufferedReader</a></li>
<li><a href="buffered-writer-java.html" class="closed">Java BufferedWriter</a></li>
<li><a href="string-reader-java.html" class="closed">Java StringReader</a></li>
<li><a href="string-writer-java.html" class="closed">Java StringWriter</a></li>
<li><a href="print-writer-java.html" class="closed">Java PrintWriter</a></li>
</ul>
</li>
<li>
<div class="link">Additional Topics<i class="fa fa-chevron-down"></i>
</div>
<ul class="submenu collapse">
<li > <a href="scanner-class-java.html" class="closed">Java Scanner Class</a> </li>
<li><a href="type-casting-java.html" class="closed">Java Type Casting</a></li>
<li><a href="autoboxing-and-unboxing-java.html" class="closed">Java Autoboxing and Unboxing</a> </li>
<li><a href="lambda-expression-java.html" class="closed">Java Lambda Expression</a></li>
<li><a href="generics-java.html" class="closed">Java Generics</a></li>
<li><a href="file-class-java.html" class="closed">Java File Class</a></li>
<li><a href="wrapper-class-java.html" class="closed">Java Wrapper Class</a></li>
<li><a href="command-line-arguments-java.html" class="closed">Java Command line Arguments</a></li>
</ul>
</li>
</ul>
</section>
<br>
<section class="para">
<!-- <div class="but mt-4">
<button class="btn btn-md btn-primary "><a href="../dsa.html"><i
class="fas fa-arrow-left "></i> Back to Index</a></button>
</div> -->
<!-- breadcrumb -->
<nav class="bdcrumb" id="bdcrumb" aria-label="breadcrumb">
<ul class="breadcrumb">
<li class="breadcrumb-item"><a href="../tutorials.html"><i class="fas fa-home"></i></a></li>
<li class="breadcrumb-item"><a href="../java.html#languages">Java Index</a></li>
<li class="breadcrumb-item active" aria-current="page">Java Annotations</li>
</ul>
</nav>
<!-- breadcrumb ending -->
<!-- para-->
<div class="wrapper">
<h3>Java Annotations</h3>
<p>In this tutorial, we will learn what annotations are, different Java annotations, types of Java Annotations and how to use them with the help of examples.</p>
<p>Java Annotations allow us to add metadata information into our source code, although they are not a part of the program itself. Annotations were added to the java from JDK 5. Annotation has no direct effect on the operation of the code they annotate (i.e. it does not affect the execution of the program).</p>
<p>An annotation always starts with the symbol @ followed by the annotation name. The symbol @ indicates to the compiler that this is an annotation.</p>
<p>For e.g. @Override
Here @ symbol represents that this is an annotation and the Override is the name of this annotation.</p>
<h4>Where we can use annotations?</h4>
<p>Annotations can be applied to the classes, interfaces, methods and fields. For example the below annotation is being applied to the method.</p>
<pre><code>
@Override
void myMethod()
{
//Do something
}
</code></pre>
<h4>Use of Annotations</h4>
<ul><li><strong>Instructions to the compiler:</strong> There are three built-in annotations available in Java (@Deprecated, @Override & @SuppressWarnings) that can be used for giving certain instructions to the compiler.</li>
<li><strong>Compile-time instructors:</strong> Annotations can provide compile-time instructions to the compiler that can be further used by sofware build tools for generating code, XML files etc.</li>
<li><strong>Runtime instructions:</strong> Some annotations can be defined to give instructions to the program at runtime. These annotations are accessed using Java Reflection.</li></ul>
<h4>Different Formats of Annotations</h4>
<ul><li><strong>Marker Annotations:</strong> The only purpose is to mark a declaration. These annotations contain no members and do not consist any data.
<p>Example :</p>
<pre><code>
@AnnotationDemo()
</code></pre></li>
<li><strong>Single value Annotations:</strong> These annotations contain only one member and allow a shorthand form of specifying the value of the member. We only need to specify the value for that member when the annotation is applied.
<p>Example :</p>
<pre><code>
@AnnotationDemo("testing")
</code></pre></li>
<li><strong>Multiple element Annotations:</strong> These annotations contain multiple elements separated by commas.
<p>Example :</p>
<pre><code>
@AnnotationDemo(element1 = "value1", element2 = "value2")
</code></pre></li>
<li><strong>Type Annotations:</strong> Before Java 8, annotations could be applied to declarations only. Now, type annotations can be used as well. This means that we can place annotations wherever we use a type.
<p><b>Example :</b></p>
<pre><code>
<strong>Constructor invocations</strong>
new @Readonly HashSet<>()
<strong>Type definitions</strong>
@NonNull String s;
<strong>Type casts</strong>
newStr = (@NonNull String) s;
<strong>extends and implements clause</strong>
class Warning extends @Localized Message
<strong>throws clause</strong>
public String readMethod() throws @Localized IOException
</code></pre></li></ul>
<h4>Types of Annotations</h4>
<ol><li><strong>Built-In Annotations</strong><ul><li>@Deprecated</li>
<li>@Override</li>
<li>@SuppressWarnings</li>
<li>@SafeVarargs</li>
<li>@FunctionalInterface</li></ul></li>
<li><strong>Meta Annotations</strong><ul><li>@Retention</li>
<li>@Documented</li>
<li>@Target</li>
<li>@Inherited</li>
<li>@Repeatable</li></ul></li>
<li><strong>Java Custom Annotations</strong></li></ol>
<h4>Built-In Annotations</h4>
<ol><li><strong>@Deprecated</strong>
<ul><li>It is a marker annotation.</li><li>It is included in java.lang package.</li><li>The Javadoc @deprecated tag should be used when an element has been deprecated.</li></ul></li>
<strong>Example :</strong>
<pre><code>
class Codemistic
{
/*
-> @deprecated
-> This method is deprecated and has been replaced by newMethod()
*/
@Deprecated
public static void deprecatedMethod()
{
System.out.println("Deprecated method");
}
public static void main(String args[])
{
deprecatedMethod();
}
}
<strong>Output :</strong>
Deprecated method
</code></pre>
<li><strong>@Override</strong>
<ul><li>The @Override annotation specifies that a method of a subclass overrides the method of the superclass with the same return type, method name and parameter list.</li><li>It is a marker annotation that can be used only on methods.</li></ul>
<strong>Example :</strong>
<pre><code>
class Base
{
public void Display()
{
System.out.println("Base display()");
}
}
class Derived extends Base
{
@Override
public void Display()
{
System.out.println("Derived display()");
}
}
class Main
{
public static void main(String args[])
{
Base t1 = new Derived();
t1.Display();
}
}
<strong>Output :</strong>
Derived display()
</code></pre>
</li>
<li><strong>@SuppressWarnings</strong>
<ul><li>It informs the compiler to suppress specified compiler warnings.</li><li>The warnings that can be suppressed are compiler-specific but there are two categories of warnings: deprecation and unchecked.</li><li>The warnings to suppress are specified by name, in string form.</li></ul>
<strong>Example :</strong>
<pre><code>
class DeprecationTest
{
@Deprecated
public void Display()
{
System.out.println("Deprecationtest display()");
}
}
public class SuppressWarningsTest
{
// If we comment below annotation, program generates warning
@SuppressWarnings({"checked", "deprecation"})
public static void main(String args[])
{
DeprecationTest d1 = new DeprecationTest();
d1.Display();
}
}
<strong>Output :</strong>
Deprecationtest display()
</code></pre>
</li>
<li><strong>@SafeVarargs</strong>
<ul><li>We use this annotation on methods or constructors that cannot be overridden.</li><li>A programmer assertion that the body of the annotated method or constructor does not perform potentially unsafe operations on its varargs parameter.</li><li>Before Java 9, we could use this annotation only on final or static methods because they cannot be overridden. We can now use this annotation for private methods also.</li></ul>
<strong>Example :</strong>
<pre><code>
import java.util.ArrayList;
import java.util.List;
public class JavaExample
{
@SafeVarargs
private void print(List... names)
{
for (List<String> name : names)
{
System.out.println(name);
}
}
public static void main(String[] args)
{
JavaExample obj = new JavaExample();
List<String>list = new ArrayList<String>();
list.add("Adarsh");
list.add("John");
list.add("Megan");
obj.print(list);
}
}
<strong>Output :</strong>
Adarsh
John
Megan
</code> </pre>
</li>
<li><strong>@FunctionalInterface</strong>
<ul><li>Java 8 first introduced this @FunctionalInterface annotation.</li><li>A functional interface can have only one abstract method.</li><li>In case more than one abstract methods are present, the compiler flags an ‘Unexpected @FunctionalInterface annotation’ message.</li></ul>
<strong>Example :</strong>
<pre><code>
@FunctionalInterface
interface Square
{
int calculate(int x);
}
</code></pre>
</li></ol>
<h4>Meta Annotations</h4>
<ol><li><strong>@Retention</strong>
<ul><li>The @Retention annotation specifies the level up to which the annotation will be available.</li></ul>
<strong>Syntax :</strong>
<pre><code>
@Retention(RetentionPolicy)
</code></pre>
<p><strong>Types of Retention Policy</strong></p>
<ul><li>SOURCE: Annotations will be retained at the source level and ignored by the compiler.</li><li>CLASS: Annotations will be retained at compile time and ignored by the JVM.</li><li>RUNTIME: These will be retained at runtime.</li></ul>
<li><strong>@Documented</strong>
<ul><li>It is a marker interface that tells a tool that an annotation is to be documented.</li></ul>
<strong>Syntax :</strong>
<pre><code>
@Documented
</code></pre>
</li>
<li><strong>@Target</strong>
<ul><li>It is designed to be used only as an annotation to another annotation.</li><li>This argument specifies the type of declarations to which the annotation can be applied.</li></ul>
<strong>Syntax :</strong>
<pre><code>
@Target(ElementType)
</code></pre>
<table width="99%" border=2>
<tr>
<td width="50%"><strong>Target Constant</strong></td>
<td width="50%"><strong>Annotations Can be Applied To</strong></td>
</tr>
<tr>
<td width="50%">ElementType.ANNOTATION_TYPE</td>
<td width="50%">Another annotation</td>
</tr>
<tr>
<td width="50%">ElementType.CONSTRUCTOR </td>
<td width="50%">Constructor</td>
</tr>
<tr>
<td width="50%">ElementType.FIELD </td>
<td width="50%">Field</td>
</tr>
<tr>
<td width="50%">ElementType.LOCAL_VARIABLE</td>
<td width="50%">Local variable</td>
</tr>
<tr>
<td width="50%">ElementType.METHOD</td>
<td width="50%">Method</td>
</tr>
<tr>
<td width="50%">ElementType.PACKAGE</td>
<td width="50%">Package</td>
</tr>
<tr>
<td width="50%">ElementType.PARAMETER</td>
<td width="50%">Parameter</td>
</tr>
<tr>
<td width="50%">ElementType.TYPE</td>
<td width="50%">Class, Interface, or enumeration</td>
</tr>
</table>
</li>
<li><strong>@Inherited</strong>
<ul><li>It is a marker annotation that can be used only on annotation declaration.</li><li>@Inherited causes the annotation for a superclass to be inherited by a subclass.</li></ul>
<strong>Syntax :</strong>
<pre><code>
@Inherited
</code></pre>
</li>
<li><strong>@Repeatable</strong>
<ul><li>An annotation that has been marked by @Repeatable can be applied multiple times to the same declaration.</li><li>The value defined in the @Repeatable annotation is the container annotation. The container annotation has a variable value of array type of the given repeatable annotation.</li></ul>
<strong>Example :</strong>
<pre><code>
import java.lang.annotation.Repeatable;
@Repeatable(Cities.class)
public @interface City
{
String name();
}
public @interface Cities
{
City[] value();
}
</code></pre>
</li>
</ol>
<h4>Custom Annotations</h4>
<ul><li>User-defined annotations or Custom annotations can be used to annotate program elements, i.e. variables, constructors, methods, etc.</li></ul>
<strong>Syntax :</strong>
<pre><code>
[Access Specifier] @interface<AnnotationName>
{
DataType <Method_Name>() [default value];
}
</code></pre>
<strong>Example :</strong>
<pre><code>
@interface MyCustomAnnotation
{
String value() default "default value";
}
class Test
{
@MyCustomAnnotation(value = "codemistic")
public void method1()
{
System.out.println("Test method 1");
}
public static void main(String[] args) throws Exception
{
Test obj = new Test();
obj.method1();
}
}
<strong>Output</strong>
Test Method 1
</code></pre>
<hr>
</div>
<div class="but text-right">
<button class="btn btn-primary pull-right"><a href="logging-java.html">Next Topic <i
class="fas fa-arrow-right "></i><br/>Java Exception Logging</a></button>
</div><br>
</section>
<!-- footer -->
<footer >
<div class="row footer" >
<div class="logo">
<img src="../image.png" alt="" class="f-img">
<h5>LEARN CODE DISCUSS</h5>
</div>
<div class="footer-column ">
<ul class="footer-ul">
<li class="foot-header"><strong>CodeMistic</strong></li>
<li class="f-li"><a href="../index.html" class="f-links">Home</a></li>
<li class="f-li"><a href="../team.html" class="f-links">Our Team</a></li>
<li class="f-li"><a href="../terms-and-conditions.html" class="f-links">Terms of Service</a></li>
<li class="f-li"><a href="../privacy-policy.html" class="f-links">Privacy Policy</a></li>
</ul>
</div>
<div class="footer-column ">
<ul class="footer-ul ">
<li class="foot-header"><strong>LEARN</strong></li>
<li class="f-li"><a href="../cpp.html" class="f-links">C++</a></li>
<li class="f-li"><a href="../java.html" class="f-links">Java</a></li>
<li class="f-li"><a href="../python.html" class="f-links">Python</a></li>
<li class="f-li"><a href="../dsa.html" class="f-links">DSA</a></li>
</ul>
</div>
<div class="footer-column ">
<ul class="footer-ul ">
<li class="foot-header"><strong>CONTRIBUTE</strong></li>
<li class="f-li"><a href="https://docs.google.com/forms/d/e/1FAIpQLSduxk_aP9M1lqUAebQJwRXiVm2ukmdgv3T7gjBpOsr_gdeOcQ/viewform?usp=sf_link" class="f-links" target="_blank">Write an Article/Blog</a></li>
<li class="f-li"><a href="https://docs.google.com/forms/d/e/1FAIpQLSc2O28YJpxQc6kh2tKpI_Lf5MwfwGXVnhMs0g5EEXobJ4Beeg/viewform?usp=sf_link" class="f-links" target="_blank">Internships</a></li>
<li class="f-li"><a href="https://docs.google.com/forms/d/e/1FAIpQLSe9nr_Aw0fGIqm3_xWhqEzTr3UWdOFz-pvKpQvkAsosORYq9w/viewform?usp=sf_link" class="f-links" target="_blank">Videos</a></li>
<li class="f-li"><a href="https://docs.google.com/forms/d/e/1FAIpQLScOW3hJjxxwzPYL4u51oboc4T1aDtIUEIRa20hKPKu4UlcZAA/viewform?usp=sf_link" class="f-links" target="_blank">Donations/Funds</a></li>
</ul>
</div>
</div>
<div class="footer-links" style="text-align: center;">
<!-- <h7>Connect With Us</h7><br><br> -->
<a href="https://www.facebook.com/codemistic " target="_blank "><i class="icons fa-2x fab fa-facebook "
style="color:#3b5998; "></i></a>
<a href="https://www.instagram.com/codemistic " target="_blank "><i class="icons fa-2x fab fa-instagram "
style="color:#e95950; "></i></a>
<a href="https://www.twitter.com/codemistic " target="_blank "><i class="icons fa-2x fab fa-twitter "
style="color:#8accf5; "></i></a>
<a href="https://www.linkedin.com/company/codemistic " target="_blank "><i
class="icons fa-2x fab fa-linkedin " style="color:#88b3ee; "></i></a>
<a href="https://t.me/codemistic " target="_blank "><i class="icons fa-2x fab fa-telegram "
style="color:#65adda; "></i></a>
<a href="https://www.youtube.com/channel/UCS1Iv_kkgQGDwoXtvrlwlag " target="_blank "><i
class="icons fa-2x fab fa-youtube " style="color:red; "></i></a>
<p class="foot mt-3">© Copyright : <strong>CodeMistic</strong> All Rights Reserved.</p>
</div>
</footer>
<!-- CSS Scripts -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js "
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj "
crossorigin="anonymous "></script>
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo "
crossorigin="anonymous "></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js "
integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI "
crossorigin="anonymous "></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<script src='../side.js'></script>
</body>
</html>