Tuesday, 4 February 2014

Write a simple JAVA program in eclipse

This is another in a series of videos that I am producing for my YouTube channel HowDoYouDoStuff.

In this video, I am going to show you how to write a simple JAVA program in eclipse

You can read the transcript below.

HTML 5 Player


Hi guys. I'm Phil Stirpe.

In this video, I am going to show you how easy it is to write a simple JAVA program in eclipse.

In an earlier video, I showed you how to write a simple program in notepad and then compile it using javac. I then recorded a video showing how to install eclipse.

Well now it's time to pull those things together.

Using an integrated development environment - IDE such as Eclipse is far preferable to trying to develop using notepad and the javac compiler.

So here is Eclipse. Before I get started, I'll just rearrange the windows to suit.

To start with, I'll drag the outline window down to sit below the Project Explorer.

And then the Task list window to sit at the bottom with the other windows.

There is no 'right way' to lay out your windows. It's a matter of preference.

I find that this layout gives me more space to work with.

I have already selected a workspace. In other words, I chose a working directory when I launched eclipse.

Let me show it to you. It's here on the Desktop in a folder named Java Demo.

So any project that I create will be created in this folder.

I'll switch back into Eclipse so that I can create a project.

If I click on File / New menu, you will see that I can create a variety of project types.

Alternatively, I can right-click in the project explorer to receive the same options.

I'll choose a straight forward Project option.

In the New Project wizard, I'll select Java Project and then click Next.

I'll give the project the name of TestProject and then confirm that the target execution environment is of JavaSE-1.7.

I don't need to worry about these other options for this demo so I'll click Next.

New projects contain a src folder where source (code) files will be stored.

You can add more if you wish but I am happy to go with the defaults. So I'll click Finish.

As I am creating a Java project, Eclipse now offers to use an appropriate perspective. A perspective is essentially a set of predefined views, layouts and menu structures appropriate to a particular use case.

By agreeing to use the predefined Java perspective, you are telling Eclipse to present you with only those features that are useful to a Java developer. If you were working on a Maven project, then you might choose a different perspective.

As you can see, the Java perspective placed the Outline window and Task List on the right of the screen, so I'll just put them back where I prefer them.

OK, I am ready to start. And I can start by expanding the project node in the Project Explorer.

As you can see, in addition to the src folder, there is also a folder containing the JavaSE-1.7 library as that is the environment that I selected earlier.

Actually, these are quick links. In other words, the project is referencing the libraries that I require rather than copying them into my project.

Right then. It is time to create my class. So I can right-click on the src folder and select New / Class from the menu.

When creating classes in Java, you are advised to group them together into Packages. Packages are simply a mechanic to help you identify and locate related classes.

I'll define a package named demo.classes and then name my class HiGuys.

The New Java Class dialog has a number of useful features.

For example, it can add a main method to your class if you wish.

Not all classes need a main method. For example, servlets in a web application don't need them as they are instantiated in response to inbound requests.

However, as I want to execute my code from the command line, my class will require a main method.

It is worth mentioning at this point that of course classes can inherit from others and by default, Eclipse has your new classes inherit from the object class which can be found in the java.lang package.

If you want your class to inherit from another class, you need simply enter its name in here.

Right then. I'll click the Finish button which results in Eclipse creating my class file.

As you can see, it has written a class named HiGuys and placed a method named main within it.

All I want to do in this program is output a simple line of text to the console.

For that I will need to call the System.out.println method.

So i'll go ahead and type that in.

Now then. Before I proceed, them me show you a useful feature called Code Templates.

These are predefined snippets of code that have an associated shortcut.

So let me remove that line and then type in sysout followed by the Ctrl and Space keys.

Eclipse replaces my shortcut with the associated code.

Very nice.

There are many of these Code Templates defined and in fact, you can even create your own.

Now there's a great subject for a video. I'll put that on the list.

If you watched my earlier video, you will know that I have never written a Hello World app. Despite it being a tradition. Instead, I am going to output the message "Hi Guys!".

Once you have written your code, you are bound to want to test it.

Well, I'll do that now.

I'll save my code file first and then right-click anywhere in the code window.

In the context menu, I can click Run As and then select Java Application.

This causes Eclipse to compile the code and then run it with any output being displayed in the Console window.

That is to say, the Console window in Eclipse and not the Command Prompt where you might typically run a Console application.

As you can see, my output of Hi Guys! has been echoed to the Console.

Now a question to consider here is, did Eclipse actually compile my code and generate a class file which it in turn executed or did it interpret my code in some way?

Well if I switch into File Explorer and take a look at my project folder, I can find the source file ok.

And if I navigate to /bin/classes/demo I will also find my class file. That proves that my class was indeed compiled prior to execution.

There you have it.

In this video, I have shown you how easy it is to write a simple JAVA program in eclipse.

Thanks for watching and please feel free to comment on my blog (www.philipstirpe.com) and Facebook page (www.facebook.com/philip.stirpe.tutorials). Perhaps you could suggest more video topics? Most of all, don't forget to subscribe to keep up with my videos as I release them.

Bye for now.

Flash Player

See you soon

Phil Stirpé
"I don't do average!"

No comments:

Post a Comment