File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why should I use annotations and what are those? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why should I use annotations and what are those?" Watch "Why should I use annotations and what are those?" New topic
Author

Why should I use annotations and what are those?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi everybody, I am Chaitanya, I am new to annotations, I don't know anything about it, I searched the web, I dint understand the usage, can anyone give some examples where to use annotation, where in programming one can come across the usage of annotations and what is meant by annotations.

Actually I want to learn hibernate, people advised me to learn few thing, in those annotations is one part. So please can anyone help me with annotations.

I will be very much thankful to you people and thanks in advance. Have a nice day.


Love all, trust a few, do wrong to none.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Start here: http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Thank you Mr.Lester, first I went to that site only, I dint understand anything and I came here. They are not talking about where to use, they are focusing on how to use and its not in detailed manner.

Please help me.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Annotations are a manner in which you can add meta-data to your code. For instance that a methods shouldn't be used anymore (@Deprecated). You could see annotations as a kind of flag with information. Another program can spot those flag and perform logic based on that. For instance annotations are heavily used for ORM with JPA to create a database model.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

I do not see any great use of annotations in java, apart from they used for suppress warnings in jdk5 and above, and for showing deprecated methods.

We generally use these in EJB3, JPA, Hibernate, since these saves developers few lines of code, and we have predefined annotations available in the jar files.

SCJP 5, SCWCD 5
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Prabhakar Reddy Bokka wrote:I do not see any great use of annotations in java, apart from they used for suppress warnings in jdk5 and above, and for showing deprecated methods.

We generally use these in EJB3, JPA, Hibernate, since these saves developers few lines of code, and we have predefined annotations available in the jar files.

Maybe that annotations for Java SE aren't that useful but for Java EE they're great. The don't save a few lines of code but allow us to configure servlets, components, JPA settings, do validation directly on the entity classes and much more. Not to mention that they replace lot's of xml files and overall reduce the complexity of the code. With them coding would be much harder.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

I agree with you in reducing lot's of xml files in configuration. But i feel it creates more complexity in understanding code and configurations. It all depends on the perception of the developer.

If the developer is new to annotations, they definitely feel a lot of complexity in understanding them.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Prabhakar Reddy Bokka wrote:I do not see any great use of annotations in java, apart from they used for suppress warnings in jdk5 and above, and for showing deprecated methods.

We generally use these in EJB3, JPA, Hibernate, since these saves developers few lines of code, and we have predefined annotations available in the jar files.

How can you not see the use, then list a bunch of uses?

Spring uses annotations, various frameworks use annotations, Lombok uses annotations, they're all over the place besides EJBs and JEE stuff. They are of tremendous value under some circumstances.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
@Override is a great timesaver during development, and much more important than either @Deprecated or @SuppressWarnings (which are mere conveniences).
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

David Newton wrote:
Spring uses annotations, various frameworks use annotations, Lombok uses annotations, they're all over the place besides EJBs and JEE stuff. They are of tremendous value under some circumstances.


I feel they all are using annotations at the cost of complexity to understanding.
Btw, could you please eloborate the uses?? apart from programatic configurations and less code to write(since predefined annotations provided).
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Prabhakar Reddy Bokka wrote:I feel they all are using annotations at the cost of complexity to understanding.

It's a different *kind* of complexity, but I wouldn't call it *more* complexity.

While I still prefer XML configuration for some things, without good IDE support, maintaining a separate configuration file is *more* work, and doesn't provide compile-time type safety like annotations do. Not only are there more artifacts, but configuration isn't localized to the code that uses it.

Btw, could you please eloborate the uses??

Well, I kind of already did, but okay.

Consider Project Lombok. Consider Spring Contracts. Consider being able to define method access control via annotations. Consider field validation driven by annotations. Consider test framework annotations (both JUnit and TestNG use them in similar, but different, ways). State machine transitions. Consider annotating code for completely arbitrary business or documentation reasons. Consider annotating code for any run-time class-loading tricks (like Lombok, but it can be *anything*, and it's *powerful*).

The uses of annotations are limited only by your imagination and technical skill.

Consider Gag annotations.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
David Newton wrote: . . . Gag annotations.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(Yeah--I already dropped it into a couple of projects--I love it.)
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi everybody, thank you all for the replies, really saying I dint understand the user, I think it is because you all experts, you all are talking in a higher level, I cant understand that because I am new to annotations.

Please if you don't mind can anyone give a general example

Suppose take an example. Suppose some one asked me why to use charAt(int) function. I explain the person like this, suppose you have a string variable and you want to check it whether it is an integer or string so that you can convert it into integer using the parseInt(String) method of Integer class. So you have to check each and every character in the string from first to end of string, so to read each and every character you need a function, that is charAt(int) function, for this purpose you can use it.

I ll say it in this way.

Please can anyone tell me such kind of example. Please don't mind and thank you all in advance.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Override Example

Suppose you're writing a class that extends another class.

The new class overrides a method of the superclass.

That method can be annotated with the @Override annotation.

If the method is overridden incorrectly, for example, you leave out a parameter, or change the return type, it no longer overrides the superclass's method. If it's annotated with @Override, the compiler will complain and give a compilation error saying that the method *doesn't* override anything.

So in this case the annotation provides a compile-time check to see if what you *intended* is what you actually *implemented*.

Hibernate Example

See Using @Transactional.

For example, we want to make every method within a service run within a transaction. Along with some Spring configuration, it's as simple as adding the annotation.That annotation also takes arguments, thusly:Not sure what else to tell you: until you start using them you won't understand why you'd use them.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Thank you David for your reply and patience in reading my post, I understood the first example, I dint understood the second one, as you said, may be I have to start using annotation.

Once again thanks a lot David, have a good day.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why should I use annotations and what are those?
 
Similar Threads
Difference between Java 5 adn Java 1.4
HttpSessionBindingListner
Annotation dilema
CollectionOfElemetns
CollectionOfElements