Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Annotation question.

 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose that "Enumerated" is an annotation, which has only one parameter as below:



In this case, EnumType is a class, which has some final variables like: EnumType.ORDINAL and EnumType.STRING

For sure, we can 'call' to this annotation as below:

@Enumerated(EnumType.ORDINAL) ....


But, how can we use it as below,

@Enumerated(ORDINAL) .....

How does the compiler know that ORDINAL is from EnumType, in that case? Is this really compile-able?

I found this type usages of annotations from many books.

(Currently I have no such environment to test this my self).



Source:
The EJB 3.0 JPA Specification
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using static imports you should be able to do what you want.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Using static imports you should be able to do what you want.


Yes I know that, but did you mean that we can't access it without using static importing statement?

Thanks Rob,
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler needs to know where to find it, so you need to:
1) import it
2) use a larger part of the full name*

You can't expect the compiler to check all constants of classes (or all enums), and just pick one if there are multiple matches, now can you?
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob, I asked this question, because I've seen this in many books.

Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic