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.
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.
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 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.
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).
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.
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.
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*.
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.