Eclipse Tutorial

< Part 1: Eclipse Project Framework

Part 2: Creating Java Elements

Part 3: Useful Programming Functions >

Part 2: Creating Java Elements

"Even the longest journey begins with a single step". Chinese proverb.

Once you have set up a new project, it is time to learn how to create Java elements. To follow these steps you should switch now to the "Java Perspective" using "Window > Perspective > Java". The Java Perspective contains the most useful views and editors to create Java programs.

Just by right clicking on your previously defined source folder (i.e., "src") in the Package Explorer view, a contextual menu will show up. Choose "New >" in that menu to view the Java elements that you can create.

You can also create some of the following elements by clicking the menu icons at the top of the screen.

Java Class

Java classes are the ".java" code files which are later compiled into ".class" files. These Java files should be stored inside your previously created source folder (i.e., src). Click "New > Class" to open the class creation window.

 

 

The source folder specified should be the recently created source folder (i.e., src). If you don't specify a package containing your Java classes, they will be stored under a default package. The last mandatory field that you should specify before creating your Java class is its name. Java conventions suggest that a class name should start with a capital letter. In this tutorial, the name "ExampleClass" has been used.

There are also other modifiers that can be easily added to a class from the very moment of its creation. Nevertheless, these elements may be added manually at later stages of development. If our class is intended to extend another class you should specify this parent class in the "Superclass" field. Notice that the "Browse..." button will be of great help for this purpose. Besides only single heritage is supported in Java, you can pick up various interfaces that your new class may implement. Again, the "Browse..." button simplifies this task.

If your class is intended to be a main class (i.e., the starting execution point), a main method should be added. This can be done just by checking the appropriate option. You can also implement this way the constructors from the superclass and all inherited abstract methods. This last option is very useful if you want your class to be instantiated, as every abstract method should be implemented.

Notice that ".class" files are only visible in the Resource Navigator view. As the Java Perspective does not open that Navigator view by default, you won't see the output compiled folders in the Package Explorer view. But, once a ".java" file is written and saved, an associated ".class" file is created through compiling the previously defined code.

File

Files are usually created to store some notes and information. You would also need to create files when defining elements such as configuration files, build files, etc. Creating a new file is as easy as clicking "New > File", selecting the appropriate project and folder where you wish to create the file, name it and press the "Finish" button. By default, generic files are open with the text editor.

Folder

Folders are used to store archives. Notice that a "Source Folder" is not exactly the same as a "Folder", as it will be explained later. It is a suggested programming practice to create a source folder to store the source files (i.e., src), an output folder to store the compiled code (i.e., bin) and a folder to store every related documentation (i.e., docs). Folders are created just by specifying the parent folder or project, as well as the new folder name.

 

 

Interface

Interfaces are specific cases of Java classes, which are intended to be implemented by other classes. An interface usually expresses what a program should do at a higher level of detail, while the implementor should code the lower level details. An interface may only contain attributes and method signatures. The interface creation process is similar to the normal class creation process already detailed. It should be taken into account that an interface does not implement any interface, but it may extend other interface.

Package

Packages are declared to store Java archives. A package name usually consists of some parts separated by a "." Each one of these parts will be a directory in the file system. Classes created inside a new package will automatically have a "package" declaration added.

Scrapbook Page

Scrapbook Pages are an easy way of testing code snippets before adding them to a final program. Just create a scrapbook page inside the desired folder and type your code inside it.

Eclipse 3.0 Update

In Eclipse 3.0 scrapbook pages are not directly shown in the contextual creation menu. To create a new scrapbook page select "New > Other > Java > Java Run/Debug > Scrapbook Page". Also, scrapbook pages in Eclipse 3.0 have text assist functions enabled.

 

Before trying to execute this code it is necessary to import the classes used. Right click any part of the editor and select "Set Imports" from the contextual menu. Then, the needed types and packages should be added.

In order to execute the recently created code it is necessary to select it by clicking the left mouse button and dragging the mouse till the full code is selected. Then, right click and select "Execute". The standard output of this process will be shown inside the "Console" view, while other messages such errors will be shown inside the scrapbook page.

After the test has been completed, you should click the "Stop Evaluation" button.

As the scrapbook page editor does not provide as much programming support as the Java code editor, it is a good practice writing your Java code outside this scrapbook page and pasting it inside later.

 

Source Folder

Source folders are a special kind of folders which is intended to store the source code files (i.e., the ".java" files). These code files will be automatically compiled into ".class" files. As every Java project should have a source folder, it is a good practice organising it from the first moment, as explained in the previous section of this tutorial.

 

< Part 1: Eclipse Project Framework

Part 2: Creating Java Elements

Part 3: Useful Programming Functions >

Google
 
Web eclipsetutorial.forge.os4os.org

Return to index

A la versión española


 

This site is optimised for a 1024x768 screen resolution.

Created by Enrique Serrano Valle

This site contents are under the Creative Commons Attribution 2.0 license.

Eclipse trademark and logos appear according to Eclipse legal conditions. For more information check http://www.eclipse.org/legal/main.html

Java is a registered trademark of Sun Microsystems Inc.