Personal notes for learning Java enumeration and annotations

Keywords: Spring JDK

1. Enumeration

public class TestSeason {
	public static void main(String[] args) {
		Season spring = Season.SPRING;
		System.out.println(spring);
		spring.show();
		System.out.println(spring.getSeasonName());
	}
}
//Enum Class
class Season{
	//1. Provide the properties of the class and declare it private final 
	private final String seasonName;
	private final String seasonDesc;
	//2. Properties declared final, initialized in the constructor.
	private Season(String seasonName,String seasonDesc){
		this.seasonName = seasonName;
		this.seasonDesc = seasonDesc;
	}
	//3. Call properties through a common method
	public String getSeasonName() {
		return seasonName;
	}
	public String getSeasonDesc() {
		return seasonDesc;
	}
	//4. Create objects of enumerated classes: declare objects of classes public static final
	public static final Season SPRING = new Season("spring", "In the warm spring, flowers are coming out with a rush");
	public static final Season SUMMER = new Season("summer", "Dog Days of Summer");
	public static final Season AUTUMN = new Season("autumn", "Fresh autumn weather");
	public static final Season WINTER = new Season("winter", "Snow gleams white");
	@Override
	public String toString() {
		return "Season [seasonName=" + seasonName + ", seasonDesc="
				+ seasonDesc + "]";
	}
	public void show(){
		System.out.println("This is a season");
	}
}

 

2. Notes

 

1). Basic comment types built into JDK (3)

1. @override: Restricts override of parent methods, this comment can only be used for methods

(2). @Deprecated: Used to indicate that a program element (class, method, etc.) is obsolete

public class Test {
	public static void main(String[] args) {
		
		tests();
	}
	@Deprecated
	public static void tests(){
		
	}
}

3. @SuppressWarnings: Suppress compiler warnings

@SuppressWarnings({ "unused", "rawtypes" })
List list=new ArrayList();

 

2). Custom Note Types

public @interface T {

}

 

3. Comment on notes (4)

Retention: Can only be used to modify an Annotation definition to specify how long an Annotation can be retained

Target: Used to modify the Annotation definition, specifying which program elements the modified Annotation can be used for

3. Documented: An Annotation class used to specify that the Annotation class decorated with that element will be extracted into a document by the javadoc tool

IV. Inherited: The Annotation modified by it will be inheritable

 

4. Obtaining annotation information using reflection (referred to in the reflection section)

Posted by mwalsh on Sun, 02 Feb 2020 10:34:17 -0800