Coding in Eclipse


What is a perspective?

In Eclipse, a perspective is a way to organize and view the files associated with your program. There are different perspectives available in Eclipse. The one you want to use for your projects is the “Java” perspective, which is Eclipse’s default perspective. Don’t confuse the “Java” perspective with the “Java Browsing” perspective or the “Java Type Hierarchy” perspective, as each one is different. If you want to change to a particular perspective select Window → Open Perspective, and then choose appropriate perspective.

Resetting a perspective

You may notice that certain components of the various perspectives, for example the Console window, or the Package Explorer, can be closed via the X widget in their upper right corners. What happens if you accidentally close the Console, for example? How will you see what’s going on? If this happens, just reset your perspective by selecting Window → Reset Perspective and clicking “OK” when prompted to confirm. Your perspective will be restored to its original state with all of its original windows/frames.

Creating a Project

Now that you’ve got Eclipse up and running, it’s time to create your first Java project. To do this, select File → New → Java Project. After doing so, you’ll see a window like this:

Create a Java Project (window prompt)

The project name can be whatever you like, and it is the only value you need to provide. Your project will be created in the workspace associated with Eclipse. Click “Finish” and you will see a window like this one:

'MyProject' project on workbench, located in 'Package Explorer'

Adding a New File to a Project

Now that you’ve created your project, you will want to create a Java file and add it to your project. A Java file is a file with the .java extension, a plaintext file in which you will write your code. To create a new Java file, right click on your project and select New → Class. You'll see the following window:

New Java Class (window prompt)

In the “Name” field, provide the name (e.g., HelloWorld) for the file you want to create. In addition, in the section "Which method stubs would you like to create?" select public static void main(String[] args). Once you have completed your selections, click “Finish.” This is the window you will see:

'' class on workbench

You will see that a class HelloWorld with a main method has been created for you.

Saving, Compiling, and Running Java Code

Ready to write some code? Paste the following line into your public static void main(String[] args) method, in between the { and }:

System.out.println("Hello world!");

The whole method should look like this:

public static void main(String[] args) {
    System.out.println("Hello world!");