Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Create custom annotation

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

How can we create a custom or our own annotation? What all we need to install? Are there any plugins in eclipse for the same? Please guide.

Thanks,
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create your own annotations like this:

See this tutorial for more information.

You don't need to install anything special to create your own annotations. They are a standard part of the Java language since Java version 5.
 
Sheriff
Posts: 21971
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just don't expect the compiler to do anything special with them. There are only three annotations recognized by Oracle's compiler, and it's not a coincidence they are all located in the java.lang package; they are @Override, @Deprecated and @SuppressWarnings. Some IDEs add support for their own annotations to their own compilers. An example is the @NotNull annotation of IntelliJ IDEA.
 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went through the tutorial but it was not clear to me. Rob said that there is a @NotNull annotation by IntelliJ, how can we create such an annotation ourselves.
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have not really described what you are trying to do. This makes it almost impossible to help you, as Annotations can cover chapters in books. But to give you a very simple example, I could create a simple annotation like:

And then use it in my application as:
 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew,

Thanks for the reply, could you please let me know how can I learn annotations in deep, are there any books or any tutorials available? I am really curious to learn it but i could not find anything satisfactory on internet.

Thanks for your guidance.
 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please guide
 
Andrew Monkhouse
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the best books (and not only for annotations - it is one of the best books a Java developer can have) is Effective Java (2nd Edition). It has several sections on annotations, and effectively shows how to create a framework such as JUnit using annotations.

Java (A Beginner's Tutorial) also has a chapter on annotations. However the link I gave you is for the Kindle edition, and while I know it is also available under Safari, I don't know if hard copy books are still available.
 
ice is for people that are not already cool. Chill with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic