Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Annotations Writing Your Own Annotation Adding Annotation Targets and Elements

Wei Han Liu
Wei Han Liu
11,231 Points

Adding elements to custom annotation in Java

I don't understand the question and the method to solve this. Can anybody help me?

Add two elements to the LiveTweet annotation:

  1. username, declared as a String. Set its default to an empty String.
  2. hashtags, declared as a String array. Set its default to an empty array.
LiveTweet.java
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;


@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LiveTweet {
  String desc() default "";
  String[] params() default {};


}

2 Answers

Fahad Mutair
Fahad Mutair
10,359 Points

change your element names to username and your array name to hashtags

Sagar Thakkar
Sagar Thakkar
8,814 Points
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;


@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LiveTweet {
  String username() default "";
  String[] hashtags() default {};


}