Many of your still stuck in using editors like Emacs, Editplus, DrJava or other simple editors, this is for you. Personally I’m a huge fan of Eclipse a free, open source Java IDE (Integrated Development Enviroment) written in Java, developed by IBM and backed by other large corporations such as Oracle.
The main interface
We’ll start off by looking at the main user interface in action:
(Larger version: http://www.zefhemel.com/upload/eclipse_screen1.jpg)
In the package explorer we see all currently open project with in this case the KeyTopic project opened (KeyTopic is a serverside project I’m working on for the moment). In the package explorer we see all packages we have and the classes they contain, if we expand the classes we’ll also be able to see the methods, fields etc.:
Underneath that we see the class outline, which shows the same information as you get when expanding a class in the package explorer, but this one represents the one you’re editting right now.
At the right we have the editting pane. At the top we have tabs where we can easily switch between the different files that open at the moment. A cool feature here is that you can set a maximum of tabs open, when this limit is exceeded Eclipse automatically closes tabs that haven’t been used for a while. Underneath that is the place where the code editting happens, of course fully color coded. Errors in your code are automatically underlined even as you type, but more on that later. At the bottom is the task list, when you put the words TODO: bla bla bla somewhere in your comments it will automatically be added to this list of things to do. If your project contains errors (as here) you’ll see them here.
The code editor
Now let’s have a better look at the code editor. In order to do that, let’s first create a new class in the millhouse.keytopic.beans package:
Now we get the New java class window:
We want our class to implement Comparable (and interface which can be used to compare object to other objects, very useful for sorting), we do this by clicking on the Add… button next to Interfaces. We now get this window:
As we type possible interfaces get eliminated as soon as we find the one we want we double click it and it gets added to the interfaces list, we click ok to close the interfaces window and ok again to generate the class. Since the generate inherited abstract methods checkbox is checked it will automatically generate methods for methods in the Comparable interface:
We got an error, when we move the mouse over the underline piece of code we see what’s wrong, the method should return a value of type int. We also see that there has been a task added (// TODO Auto-generated method stub) which is also added to the tasks list. We simply fix this by replacing the TODO with return 1, we save (Ctrl+s) and all errors are gone:
As you might have already concluded, Eclipse compiles your class when you save it. So there’s no additional saving step. Isn’t that slow? Well, not on my PC. Actually the compiler is compiling as you type, that also means that it finds non existent classes, non existing variables etc as you type. So let’s create a new method in our class and type some code (I won’t save or compile anything):
Without compiling (by hand) it identified 3 errors in this piece of code. When I move my mouse on the ArrayList type it’ll tell me “ArrayList cannot be resolved or is not a type” and if I move over a “a cannot be resolved”. This stuff is checked as you type. Some editors do syntax checking as you type, but the Eclipse editor does also do this more advanced kind of checking.
Ok, this is all very cool and stuff, but the main reason why we use an IDE is because we’re lazy and don’t want to remember a lot. That’s when autocompletion comes in. We already created the ArrayList which couldn’t be resolved, but we’ll add an import for that by hand (later we’ll see another way to do this). Now we want to add an element to our ArrayList, so we type list. as soon as we type the dot, this happens:
We get a full list of all methods available, but not only that, we also get help on the method currently selected (add(int index, Object element) in this case). As we type on, non-matching methods get eliminated (as with the interfaces we saw before). We can pick one by using the up and down cursor keys and press enter. Ok, we picked add(Object o):
The parenthesis are automatically added and it tells me it now expects an Object o. Also note the underlining, the current call (no arguments) is wrong, we’ll now add a string to it, when we type the “ the second “ is automatically added. Now there’s only one error left, the semicolon at the end, we’ll add that too. Ok, so when we press the dot we get a list of methods, however you can get code completions in many more occasions. For example if we type li and then press Ctrl+Space (remember Ctrl+Space is your friend!):
That’s right, the same kind of list listing all possible things you could mean, that includes variables and classes starting with li at this point. You can select one using the cursor and enter key again. You can imagine that once you get confortable with this your way of writing code changes radically. As some methods have fairly long names, I usually type just a couple of characters, press Ctrl+Space then find the one I’m looking for and press enter. If you get used to this, it saves a whole lot of typing.
We saw the argument help earlier, this works for all classes, both for the Java standard API classes and the ones you defined yourself or the ones in jars added to your project. If for some reason you don’t get the argument help, just type Ctrl+Shift+Space, it will show the options you have.
The incremental compilation is not only for finding errors in your current file easily, but if you change the name of a method in your current class (and the class is used in other classes too) you’ll see the consequences as soon as you save the file, you’ll see what gets broken and where. I will show a neater way to change method names later on.
One of the most powerful features in Eclipes is the autofix feature. Eclipse can fix some kinds of error automatically. The magic key to this is Ctrl+1. Example, we use a class not currently imported (like the ArrayList we showed before). Move our cursor to the place where the error is and press Ctrl+1:
It will show you all ways to fix this problem. First it’ll look in your classpath for a matching class, as it did here, it found out the ArrayList class is defined in java.util. But maybe you mistyped the name and meant Array or Arrays, if that’s not the case, maybe you just want to create a class called ArrayList. If you choose the later it will automatically create a class for you in the current package. In the right pane you’ll see the changes it will make to your code if you choose this option. In this case it’ll add an import, but if you choose to create the class, it’ll show what the class definition will look like. We choose to add the import and press enter, the import gets added and the problem is fixed:
The same works for non existent variables:
And when an import isn’t necessary anymore:
The quickfix is obvious:
What if a method throws a Exception that you have to catch? Well, it has two solutions for that:
Another very, very powerful feature is Eclipse’s refactoring support. What is refactoring? Refactoring is the process of making small changes to your classes, methods and package in order to improve it’s design. So let’s say you have a method with a really bad name, you could change the name in any editor, but what about all the classes calling the method? They’ll be broken, unless you use Eclipse’s refactoring support of course. Example, we have a heavily used method parse() in some class and we want to rename it to parseCode() as that is a better representation of what it does, how do we do this? Simple, we move to our method name and then right click, choose refactor and then rename:
When we click this we get this window:
If we want to know what’s going to happen, we can press the preview button, after a while we’ll see what’s going to change and where:
When we click ok, everything is changed automatically without anything getting broken.
Some other interesting refactorings:
- Moving a class to a different package, this can be done by simple dragging the class in the package explorer to a different package. All references are automatically changed.
- Extract method. It’s not uncommon that some methods get very long and hard to read over time, you can solve this by splitting your method in smaller subtasks. In Eclipse this can be done in a snap. Simply select the piece of code you want to move into a new method, right click, choose refactor and then extract method:
Eclipse will now automatically determine what variables need to be passed to this new method, eventually what to return:
Preview will, as usual, show you exactly what’s changing and where.
Method inlining. Method calls are expensive, if you find out that a method you created is only called once or twice it might not be worth to be in it’s own method. Eclipse helps you getting rid of the method by inlining the code in the right way into all methods calling it. You can do this as usual:
We now get this window:
And when we press ok, the method is inlined and removed after that.
Move static methods. You can easily move static methods from one class to the other, it’s pretty obvious how this goes so I won’t post screenshots of that.
Local renames. Through the autofix key (Ctrl+1) you can also easily locally rename certain variables (within your method):
- There are of course many more, but this are ones I use a lot.
Eclipse can also generate some code for you. A simple example of this is javadoc code, you can select a method and generate javadoc code for that:
And it’ll generate the javadoc for you (as far as possible):
Another way of accomplising the same is typing /** before the method and then pressing enter, it’ll do exactly the same.
**Getters and setters**
Creating properties in Java using getters and setters is a lot of work usually. First you have to declare the fields, then you have to write setX and getX methods for all of them. Eclipse helps you with this. First declare the fields, then rightclick, choose source and then Generate getter and setter:
You’ll get this window where you can pick exactly what getters and setters to generate:
When you click ok, the getters and setters will be generated automatically:
I can tell you, this saves you a lot of time.
There’s also the option to override methods belonging to a parent class:
When you pick that it’ll show you a list of methods you can override:
When you click ok it will generate tasks and if possible, stubs for it:
If you look more at the Source submenu after a right click, you’ll see a couple of other nice features such as the option to reformat your code (when it’s messed up) and to sort your method list alphabetically.
Eclipse offers very advanced and good debugging features, but personally I’ve never used them (I’m used to using System.out.println() when somethings not working right, probably as that’s how I debugged when I still used PHP and Perl). So I’ll leave that for you to find out. Beside that it’s of course possible to run applications from within Eclipse, but as I write server side applications mostly, I don’t use that either.
Eclipse also has Ant support, but I never use that from within Eclipse, why not? I don’t know, never really looked into it.
A very useful feature Eclipse offers is externalize strings, this allows you to put all strings (or all strings you select) into a file which can then be easily translated to different languages:
Then you can select which strings to externalize:
Probably very useful to many of us (I know it is to me).
Convinced? Good for you! Go to Ecipse.org and download the newest release, there are versions for Windows, Solaris, Linux and Mac OS X. It makes Java more fun to use and simplifies many common tasks. Many people think that using an IDE makes you not know what you’re doing. Well, some IDEs might do that, but Eclipse doesn’t. You’re still the one writing all the code, you’re the one creating the classes. All Eclipse does is helping you where possible. Sure, I might not remember in which package each class in the Java API resides because Eclipse can find it automatically for me. But, is that bad? Is that essential? I don’t think so. IDEs are not bad, they save you a hell lot of time!
What are you waiting for, go download Eclipse for free now! And remember, Ctrl+Space and Ctrl+1 are your friend.