As discussed in Chapter 1, Introduction, Eclipse's user interface consists of elements called views. The following sections will introduce the main views related to Java development.
The Package Explorer view is the default view used to display a project's contents. As the name implies, it uses the package hierarchy of the project to display its classes, regardless of the actual file hierarchy. This view also displays the project's build path.
The following screenshot shows how the Package Explorer view looks:
The Java Editor is the Eclipse component that will be used to edit Java source files. It is the main view in the Java perspective and is located in the middle of the screen.
The following screenshot shows the Java Editor view:
The Java Editor is much more than an ordinary text editor. It contains a number of features that makes it easy for newcomers to start writing Java code and increases the productivity of experienced Java programmers. Let's talk about some of these features.
As you will see in the Building and running section with more details, Eclipse builds your code automatically after every saved modification by default. This allows Eclipse to get the Java Compiler output and mark errors and warnings through the code, making it easier to spot and correct them. Warnings are underlined in yellow and errors in red.
This is probably the most used Java Editor feature both by novice and experienced Java programmers. It allows you to list all the methods callable by a given instance, along with their documentation. This feature will work by default for all Java classes and for the ones in your workspace. To enable it for external libraries, you will have to configure the build path for your project. We'll talk more about build paths further in this chapter in the Managing the project build path section.
To see this feature in action, open a Java Editor, and create a new String
instance.
String s = new String();
Now add a reference to this String
instance, followed by a dot, and press Ctrl + Space bar. You will see a list of all the String()
and Object()
methods. This is way more practical than searching for the class's API in the Java documentation or memorizing it.
The following screenshot shows the content assist feature in action:
This list can be filtered by typing the beginning of the method's name after the dot. Let's suppose you want to replace some characters in this String
instance. As a novice Java programmer, you are not sure if there's a method for that; and if there is, you are not sure which parameters it receives. It's a fair guess that the method's name probably starts with replace, right? So go ahead and type:
s.replace
When you press Ctrl along with the space bar, you will get a list of all the String()
methods whose name starts with replace. By choosing one of them and pressing Enter, the editor completes the code with the rest of the method's name and its parameters. It will even suggest some variables in your code that you might want to use as parameters, as shown in the following screenshot:
Content assist will work with all classes in the project's classpath. You can disable content assist's automatic activation by unmarking Enable auto activation inside the Preferences window and navigating to Java | Editor | Content Assist.
When the project you are working on is big enough, finding a class in the Package Explorer can be a pain. You will frequently find yourself asking, "In which package is that class again?". You can leave the source code of the classes you are working on open in different tabs, but soon enough you will have more open tabs than you would like to have.
Eclipse has an easy solution for this. In the toolbar, select Navigate | Open Type. Now, just type in the class's name, and click on OK. If you don't remember the full name of the class, you can use the wildcard characters, ?
(matches one character) and *
(matches any number of characters). You can also use only the uppercase letters for the CamelCase names (for example, SIOOBE
for StringIndexOutOfBoundsException
). The shortcut for the Open Type dialog is Ctrl + Shift + T. There's also an equivalent feature for finding and opening resources other than Java classes, such as HTML files, images, and plain text files. The shortcut for the Open Resource dialog is Ctrl + Shift + R.
You can also navigate to a class' source file by holding Ctrl and clicking on a reference to that class in the code. To navigate to a method's implementation or definition directly, hold Ctrl and click on the method's call.
Another useful feature that makes it easy to browse through your project's source files is the Link With Editor feature in the Package Explorer view, as shown in the following screenshot:
By enabling it, the selected resource in the Package Explorer will always be the one that's open in the editor. Using this feature together with OpenType is certainly the easiest way of finding a resource in the Package Explorer.
Whenever there's an error or warning marker in your code, Eclipse might have some suggestions on how to get rid of it. To open the Quick Fix menu containing the suggestions, place the caret on the marked piece of code related to the error or warning, right-click on it, and choose Quick Fix. You can also use the shortcut by pressing Ctrl + 1 with the caret placed on the marked piece of code.
The following screenshot shows the quick fix feature suggesting you to either get rid of the unused variable, create getters and setters for it, or add a SuppressWarnings
annotation:
Let's see some of the most used quick fixes provided by Eclipse. You can take advantage of these quick fixes to speed up your code writing. You can for example, deliberately call a method that throws an exception without the try/catch block, and use the quick fix to generate it instead of writing the try/catch block yourself.
catch
block that will then call the printStackTrace()
method of the thrown exception. If the method is already inside a try
block, you can also choose the Add catch clause to the surrounding try option. If the exception shouldn't be handled in the current method, you can also use the Add throws declaration quick fix.int b = 4; int c = 5; int a = performOperation(b,c);
The method call will be marked with an error that says performOperation is undefined
. To create a stub for this method, place the caret over the method's name, open the Quick Fix menu, and choose create method performOperation(int, int). A private method will be created with the correct parameters and return type as well as a TODO marker inside it, reminding you that you have to implement the method. You can also use a quick fix to create methods in other classes. Using the same previous example, you can create the performOperation()
method in a different class, such as the following:
OperationPerformer op = new OperationPerformer(); int a = op.performOperation(b,c);
Speaking of classes, quick fix can also create one if you add a call to a non-existing class constructor.
Non-existing variables can also be created with quick fix. Like with the method creation, just refer to a variable that still doesn't exist, place the caret over it, and open the Quick Fix menu. You can create the variable either as a local variable, a field, or a parameter.
Like almost everything in Eclipse, you can customize the Java Editor's appearance and behavior. There are plenty of configurations in the Preferences window (Window | Preferences) that will certainly allow you to tailor the editor to suit your needs. Appearance-related configurations are mostly found in General | Appearance | Colors and Fonts and behavior and feature configurations are mostly under General | Editors | Text Editors. Since there are lots of different categories and configurations, the filter text in the Preferences window might help you find what you want. A short list of the preferences you will most likely want to change is as follows:
As we have seen in the Java Editor description, Eclipse uses the output of the automatic compilation of the class to mark the compiler's error and warning messages throughout the code. These messages are also listed in the Problems view. The following screenshot shows how the view looks:
By default, it shows all the errors and warnings in all the open projects inside the current workspace. As you can see, every entry contains the compiler error/warning message and where the problem is located. By double-clicking on the entry, a new tab of the Java Editor, for the resource that contains the problem, will be opened (unless the tab already exists), and the caret will be placed on the problem's line.
The Problems view allows plenty of customization to make it easier to browse its entries. You can, for example, group them by severity and type by entering the View menu (white triangle in the top-right side of the view) and selecting the Group By entry.
When you have too many different projects open in your workspace, showing all the errors and warnings from all the projects might not be a good idea. Fortunately, you can customize what the Problems view will show. For this, open the Configure Contents window in the view menu.
There's a list of problem configurations on the left-hand side of the window. You can choose to show items that match either all or any of the configurations. New configurations can be added by clicking on the New button. The four default configurations cannot be removed or renamed, but they can be edited just as the ones you created.
You can modify the scope in which your configuration will search for problems and the type of problem that your configuration contains (some of these types may not refer to Java code). You can also filter problems that either contain or do not contain some text.
Another feature of the Problems view that helps organize the problems' listing is creating other views. To create another view, open the view menu and choose the New Problems view. Each view will have its own configuration, so you can create multiple views and have each one displaying one type of problem.
The Outline view provides an overview of the selected class. By default, it shows a list of the class's fields and methods. You can configure the view to hide static elements, fields, nonpublic members, and local types.
The following screenshot shows the outline of a Book
class:
The Type Hierarchy view is where you visualize a class's superclasses and extensions. To load a class's type hierarchy in this view, select the reference to the class name in the Java Editor, right-click on it, and select Open Type Hierarchy; alternatively, you can use the F4 keyboard shortcut. This tool can be useful when you have an interface and want to list all of its implementations.
The following screenshot contains the Type Hierarchy view showing the supertype hierarchy of the java.awt.Button
class:
There's an alternative to the Type Hierarchy view named Quick Type Hierarchy. Instead of loading and displaying the Type Hierarchy view, it shows the class's hierarchy in a pop-up window. The keyboard shortcut for Quick Type Hierarchy is Ctrl + T.
To see all the calls made to a specific method in the project, select a reference to the method's name in the Java Editor, right-click on it, and select Open Call Hierarchy; alternatively, use the Ctrl + Alt + H keyboard shortcut. The view provides a full, recursive hierarchy of the selected method's calls, as shown in the following screenshot:
Eclipse makes it easy to manage a class's imports. By right-clicking on the Java Editor and navigating to Source | Organize Imports, all unused imports and import classes are removed if there are references to them in the code. You can also use the Ctrl + Shift + O shortcut to organize the imports.
To illustrate how this functionality is useful, let's suppose you want to add an ArrayList
instance in your code. Instead of importing ArrayList
and using it (and you may or may not know the package name of ArrayList
from the top of your head), you can simply write:
ArrayList a;
Alternatively, you can also press Ctrl + Shift + O to add the import.
If the class's name occurs in more than one package in the current classpath, you will be prompted to choose the one to which you're referring.
You can define actions that are to be performed every time a Java source file is saved. For this, navigate to Window | Preferences in the toolbar; then, in the left menu, navigate to Java | Editor, and select Save Actions. Check the Perform the selected actions on save checkbox, and choose the actions you want to perform. It's possible to organize imports automatically to remove unused ones, add missing annotations, format the source code, and reinforce code style.
You can also define specific save actions for each project by right-clicking on the project's entry in the Package Explorer and clicking on Properties. Then, on the new window, navigate to Java Editor | Save Actions, and check Enable project specif ic settings.
Some companies and open source projects are very strict with their coding style. They have clear guidelines on when to skip lines or not, where to put whitespaces, how the indentation should look, and so on. All these guidelines can be implemented in Eclipse's Formatter tool that helps the developers to style the code without any worry. The formatter will modify your code to comply with a set of coding style rules. To run the formatter, navigate to Source | Format in the toolbar or use the Ctrl + Shift + F shortcut. You can also force Eclipse to run the formatter after file saves using Save Actions.
To edit your workspace's formatting settings, go to the preferences window (Window | Preferences), and navigate to Java | Code Style | Formatter in the left menu. The formatter's settings are saved under profiles that can be saved, imported, and exported. The Edit button brings up a huge number of settings that will suit most of your code-styling needs.
You can also define project-specific format rules. In the project properties window (right-click on the project in the Package Explorer and select Properties), navigate to Java Code Style | Formatter in the left menu, and check Enable project sp ecific settings.