Forums Register Login

Why should I use annotations and what are those?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
@Override is a great timesaver during development, and much more important than either @Deprecated or @SuppressWarnings (which are mere conveniences).
+Pie Number of slices to send: Send
 

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).
1
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

David Newton wrote: . . . Gag annotations.

+Pie Number of slices to send: Send
(Yeah--I already dropped it into a couple of projects--I love it.)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
@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.
+Pie Number of slices to send: Send
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.
Good night. Drive safely. Here's a tiny ad for the road:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 12862 times.
Similar Threads
CollectionOfElemetns
HttpSessionBindingListner
Annotation dilema
Difference between Java 5 adn Java 1.4
CollectionOfElements
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:30:12.