Eliminate Java's verbosity in a simple way

Source: Internet
Author: User
Tags finally block

What is Lombok?

Lombok is a Java Archive (JAR) file that can be used to eliminate the verbosity of Java code.

Let's look at an example of a standard Java bean. A typical Java bean typically has several properties. Each property has a accessor and mutator (getter and setter). There is usually a toString () method, A Equals () method, and a Hashcode () method.

At first glance, there is already a lot of predictable redundancy. If each attribute has a getter and setter, and is usually the case, then why do you elaborate?

Let's take a look at Lombok. To eliminate lines of code, Lombok uses annotations to identify classes and Java code blocks. In the aforementioned Java Bean example, all the getter, setter, and three other methods are implied and included at compile time.

And better yet, if you're using Eclipse or Ibm®websphere®studio application Developer (preferably if it's not working), you can integrate Lombok into Java projects and get the development results instantly. In other words, the Eclipse compiler can immediately identify the getters/setters that you are alluding to, while other Java code can refer to these methods.

The most immediate benefit, of course, is the reduction in the line of code, which is really great. Also, if you have a particular getter or setter that requires special attention, you don't have to traverse dozens of of lines of code to find this particular getter or setter. The code is also simpler and less redundant.

Lombok also allows you to simplify other parts of the code-not just Java beans. For example, you can also reduce redundant code within the try/catch/finally block and within the synchronization method.

Now, let's look at how you can do this in your own development environment.

Install Lombok

To install, this article assumes that you are using Eclipse or WebSphere Studio application Developer. If not, you can still use Lombok, but you cannot enjoy the benefits of development. However, you can still enjoy the benefits of compile-time.

First, open your browser and point the URL to http://projectlombok.org/.

At the time of this writing, a large word will appear in the upper-right corner of the page opened with this URL. This word is "Download". Click the word and start downloading the Lombok.jar. This file does not need to be uncompressed, and 99% of the files downloaded from other sites need to be decompressed.

After downloading this file, you need to execute this JAR file. Open a prompt in your operating system, go to the directory where Lombok.jar is installed, and type Java-jar Lombok.jar.

The above assumption is that there is already a Java Runtime environment (JRE) in your path. If not, you need to add it. If you want to learn how to add, you can refer to the documentation for your specific operating system.

If you are using microsoft®windows®, you can also double-click the Lombok.jar icon. Similarly, you must be able to execute jars from your graphical user interface (GUI).

Either way, you should eventually see a Lombok installation screen. This screen asks where the Eclipse or WebSphere Studio application Developer executable file is located. Its default location may be correct. But sometimes you may need to change this default location.

Click Install/update,lombok will be quickly merged into the Eclipse development environment. If Eclipse is already running, then it needs to be shut down and restarted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.