Your First Program
Most IDEs have a concept of a “Project”. This is nothing to do with the Java language, it is a convenient way of grouping together all the files created as part of one application. So start by creating a new project. In Eclipse (including EasyEclipse) we do this by by selecting the File Menu a the top left and chooing New -> Project
this brings up the New Project Wizard
Choose Java -> Java Project to bring up the following screen
Now that we have a project set up we need to add a new Class. This creates the file in which we will store our source code. To do this either use File -> New -> Class or right click on the project and choose New -> Class.
In the next dialog name the class
SayHello
- This is important, the class and the file name must be the same, even the case (upper and lower case) of the letters must be correct
Ignore the warning about the "default package" at this stage and click finish, this creates a new class file and drops you back to th editor - the place where you can edit your code.
Notice that some of the code has been generated for you, ''look at the
SayHello
example in the first part of the tutorial SayHello Example See if you can identify which parts of the code have been generated''
Copy and Paste the code from the
SayHello
example. You can either copy all of the example and replace everything that was generated, or you can copy just the parts that were not generated automatically.
Now what we need to do is compile and run the code. Compiling is something that EasyEclipse takes care of automatically, so for now we will ignore that step.
To run the code right click in the edit window (i.e. place the mouse somewhere over the code and click the righthand button) and choose Run As -> Java Applciation
Before moving on make sure you have grasped this:
- Try to change the output text.
- Try to output more lines.
Remember to Right Click and choose Run As -> Java Applciation to see the effects of your changes.
A word about code formatting
You will notice that throughout this tutorial the code examples for a specific style. For example code inside curly braces { ... } is indented. Class names begin with CAPITAL letters whilst variable and method names begin with lower case letters.
Something close to a religious war can break out from time to time about the formatting of code, some people get very upset if the { is on the same line as the if (...) { , preferring it on the line after.
Some aspects of formatting will make your life easier (such as consistent use of capitalisation and indentation) and you should pick a style and stick to it. Eclipse has a facility to reformat your code, so if it gets in a mess Eclipse can help, but it is not hard to format your code as you go.
- Login to post comments