Chapter 2. Java Development

IDEs have evolved so much along the years that it's hard to imagine writing Java code without using one. It's always possible, of course, to use text editors or very simple IDEs, but Eclipse provides so many features to make the writing of code easier and faster, that once you get used to it, chances are you will never look back.

Given the productivity gain that an IDE like Eclipse might provide, mastering it is almost as important as mastering Java programming itself. The goal of this chapter is to present the most important features Eclipse has to offer to Java programmers.

By the end of this chapter, you will be able to:

  • Create a new Java project or import an existing one
  • Navigate swiftly through the code
  • Use Java views effectively
  • Refactor code in an easy and safe way
  • Get Eclipse to generate code and fix code issues

Creating a Java project

To create a new Java project, navigate to File | New | Project. You will be presented with the New Project wizard window that is shown in the following screenshot:

Creating a Java project

Choose the Java Project option, and click on Next. The next page of the wizard contains the basic configuration of the project that you will create. The JRE section allows you to use a specific JRE to compile and run your project. The Project layout section allows you to choose if both source and binary files are created in the project's root folder or if they are to be separated into different folders (src and bin by default). The latter is the default option.

You can create your project inside a working set. This is a good idea if you have too many projects in your workspace and want to keep them organized. Check the Creating working sets section of this chapter for more information on how to use and manage working sets.

The next page of the wizard contains build path options. In the Managing the project build path section of this chapter, we will talk more about these options. You can leave everything as the default for now, and make the necessary changes after the project is created.

Creating a Java class

To create a new Java class, right-click on the project in the Package Explorer view and navigate to New | Class. You will be presented with the New Java Class window, where you will input information about your class. You can change the class's superclass, and add interfaces that it implements, as well as add stubs for abstract methods inherited from interfaces and abstract superclasses, add constructors from superclasses, and add the main method.

To create your class inside a package, simply enter its name in the appropriate field, or click on the Browse button beside it and select the package. If you input a package name that doesn't exist, Eclipse will create it for you. New packages can also be created by right-clicking on the project in the Package Explorer and navigating to New | Package. Right-clicking on a package instead of a project in the Project Explorer and navigating to New | Class will cause the class to be created inside that package.

Creating working sets

Working sets provide a way to organize your workspace's projects into subsets. When you have too many projects in your workspace, it gets hard to find the project you're looking for in the Package Explorer view. Projects you are not currently working on, for example, can be kept in separate working sets. They won't get in the way of your current work but will be there in case you need them.

To create a new working set, open the Package Explorer's view menu (white triangle in the top-right corner of the view), and choose Select Working Set. Click on New and select the type of projects that the working set will contain (Java, in this case). On the next page, insert the name of the working set, and choose which projects it will contain. Once the working set is created, choose the Selected Working Sets option, and mark your working set. Click on OK, and the Package Explorer will only display the projects inside the working set you've just created.

Once your working sets are created, they are listed in the Package Explorer's view menu. Selecting one of them will make it the only working set visible in the Package Explorer. To view more than one working set at once, choose the Select Working Set option and mark the ones you want to show. To view the whole workspace again, choose Deselect Workspace in the view menu. You can also view all the working sets with their nested projects by selecting working sets as the top-level element of the Package Explorer view. To do this, navigate to Top Level Elements | Working Sets in the view menu.

Although you don't see projects that belong to other working sets when a working set is selected, they are still loaded in your workspace, and therefore utilize resources of your machine. To avoid wasting these resources, you can close unrelated projects by right-clicking on them and selecting Close Project. You can select all the projects in a working set by using the Ctrl + A keyboard shortcut.

If you have a big number of projects, but you never work with all of them at the same time (personal/business projects, different clients' projects, and so on), you can also create a specific workspace for each project set. To create a new workspace, navigate to File | Switch Workspace | Other in the menu, enter the folder name of your new workspace, and click on OK. You can choose to copy the current workspace's layout and working sets in the Copy Settings section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.69.185