tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Annotations > Default Values

Default Values 

This example shows how to provide default values for attributes of Annotations. A simple annotation with String default attribute value is shown below,

File Name  :  
com/bethecoder/tutorials/annotations/defaults/Author.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.annotations.defaults;

/**
 * Permitted types for annotation attributes
 
 * 1. primitive type
 * 2. String
 * 3. Class
 * 4. annotation 
 * 5. enumeration 
 * 6. 1-dimensional arrays
 */
public @interface Author {
  String name() default "admin";
  String creationDate();
}
   

We can see that the name attribute is not provided for class level annotation. Here name attribute is optional. Its usage is shown below,

File Name  :  
com/bethecoder/tutorials/annotations/defaults/Source.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.annotations.defaults;

@Author(creationDate="01/01/2500")
public class Source {

  @Author(name="Author2", creationDate="02/01/2500")
  private int fieldOne;
  
  private String fieldTwo;
  
  @Author(creationDate="03/01/2500")
  public Source(int fieldOne, String fieldTwo) {
    super();
    this.fieldOne = fieldOne;
    this.fieldTwo = fieldTwo;
  }

  @Author(name="Author4", creationDate="04/01/2500")
  public int getFieldOne() {
    return fieldOne;
  }
  
  public void setFieldOne(int fieldOne) {
    this.fieldOne = fieldOne;
  }
  
  public String getFieldTwo() {
    return fieldTwo;
  }
  
  public void setFieldTwo(String fieldTwo) {
    this.fieldTwo = fieldTwo;
  }
  
}
   



 
  


  
bl  br