Eclipse – Source Auto Generate

“Let Eclipse type the code for you”, I have heard someone saying this a while back, however, as a Python programmer, looking at the length of a random Java class that easily go beyond hundreds of lines of code, I am scared.

Today, here is a how to use the `source` in Eclipse to help you generate most of your code.

First, we started a brand new Java project by creating two classes, the people which defines a class People with some basic demographic info as the attributes like name, address… And the application class to run the main function.

Actually you just need to type in private String firstname, and then for Mac user, use “option” + “command” + “down arrow”, you can simply copy and paste the line where the cursor is, then you simply need to replace the attribute name.

Now you have created 8 privates fields. However, due to security reasons, we have to specify all the attributes to be private only, then the only way to access the fields is by creating setter and getter methods. Isn’t that tedious? 8 * 2 ~ 16 different methods. In Eclipse, right click the class -> Source -> Generate Getters and Setters can dramatically save time for you.Screen Shot 2016-01-17 at 5.21.38 PM

Boom, 100 lines of got automatically generated for you! Even the comments.

Screen Shot 2016-01-17 at 5.21.56 PM

Clearly, the Source option in the menu of the project is a great place to auto generate boilerplate code. Lets see what other options do we have here.

Screen Shot 2016-01-17 at 5.24.17 PM.png

How about lets click all the “Generate Functions” and see what they do.

HashCode / Equals / Constructor using Fields / toString

This slideshow requires JavaScript.


In the end, our People class contains 4308 characters including whitespaces, and only the initial attributes were typed by my fingers, actually partially.

this.firstname = firstname; lastname, age, address, city, state, country, zip, which is only about 60 ~ 70 characters. 

Lets do the math..

60/4308 ~ 1.3% of the code was written by myself, and Eclipse manages to auto populate 98%+ of the code..

Now,  I can clearly remember why some of my colleagues were rolling their eyes while I was trying to write Java apps in VI… 🙂 LOL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s