This is Lesson 1.7 of the Java programming course.
Creating a simple Java program
In this course you will learn the major facilities of the Java language through the gradual development of a "virtual zoo". This will entail developing classes that model different types of animals, zookeepers, visitors, etc. and will incorporate a graphical user interface that allows you to simulate being the zoo's administrator.
The project will be developed in a step-by-step manner, in small incremental phases, where each phase builds on and extends the phases which came before. By the end of the course, you will have a good working knowledge of the major facilities of the Java programming language.
Once NetBeans has been installed, start it and you should see the following screen:
Uncheck the Show on Startup checkbox and then choose Help | Check for Updates, following the prompts to download and install any updates that may exist. Depending upon which updates were needed you may be prompted to restart the IDE.
Select File | New Project... after which the following dialog will appear:
Ensure that Java with Ant is highlighted in the Categories list and that Java Application is highlighted in the Projects list, then click the Next > button.
The application that you will develop in this course will model a virtual zoo, so in the New Java Application dialog overtype the Project Name field so that it states VirtualZoo
, ensure that the Create Main Class and Set as Main Project checkboxes are both checked. You can leave the Project Location entry to its default value unless you particularly wish to change it.
When you are ready, click Finish. The IDE should now look like this:
The window in the top left position shows three tabs; Projects, Files and Services, where the Projects tab is active. Within each of the tabs is an expandable tree, and each click-able element within the tree is known as a node:
- The
VirtualZoo
node at the top is the project name you created - Under the
Source Packages
node there exists the nodevirtualzoo
, which is the name of a package that contains your Java source code files. Packages provide a means of grouping files in a similar way that your computer uses folders or directories to group various files[1]. Generally, each separate package will correspond to a directory or folder on your disk. Note that packages are by convention named using all lower-case letters; NetBeans converted your entered project name for you to follow this convention - Under the
virtualzoo
package node is the Java source code file VirtualZoo.java, which was generated by NetBeans when you created the package
The larger window in the central location contains the source code for VirtualZoo
(i.e., the VirtualZoo.java file as listed under the virtualzoo
package node). This contains Java code generated by NetBeans as a starting point for your application:
- The lines that start with the characters
/*
and which continue until*/
are all comments, being notes for the benefit of programmers and which are ignored by Java. - The first actual Java statement is
package virtualzoo
. - The package statement specifies the name of the package where this particular Java file is located.
- The package statement must always be the first non-comment line in a Java source file.
- Note that the statement ends with a semi-colon – all Java statements need to end with a semi-colon unless it comprises a block, to be explained shortly.
- There then follows another set of comment lines indicating the name of the author, which will be set to your system's default value.
- The statement public class
VirtualZoo
declares that this Java source file is for a class. - You saw earlier that a class can be through of as a template or blueprint that models something. This particular class is modelling the application's starting point.
- The keyword
public
means that the class will be available for use outside of this package as well as within it. Large applications can have several packages defined, and by default objects within each package can only communicate with other objects within the same package, unless the public modifier is used. VirtualZoo
is the name of the class. Its source code will be stored in a file named VirtualZoo.java.- There is an opening brace (i.e., curly bracket) at the end of the line. This signifies the starting point of a block of other Java statements contained within the block until the closing brace character, which signifies the end of the block.
a Java block statement {
other Java statements go here
}
Some programmers prefer this format with the opening brace on its own line:
a Java block statement
{
other Java statements go here
}
This course will use the first format with the opening brace on the same line as its preceding statement.
You can therefore see that for the VirtualZoo class the block is in this format:
public class VirtualZoo { rest of class goes here }
Inside the class block is a second block that looks like this:
public static void main(String[] args) { // TODO code application logic here }
Note the following:
- The statement
public static void main(String[] args)
will seem rather cryptic if you are new to the language. For now, all you need to know is that all Java applications need at least one class containing a block with this particular signature as the starting point of the application. As you learn about Java in this course you will become familiar with the meaning of each of the terms - Inside the block the two forward slash characters mean a single-line comment follows. Again, these comments are for the benefit of the programmer and are ignored by Java
For reasons of brevity this course will often not show the multi-line comments that exist within a source file, since the course's narrative will contain a more detailed explanation. Without the multi-line comments (which you can delete if you wish) the Java source looks like this:
package virtualzoo; public class VirtualZoo { public static void main(String[] args) { // TODO code application logic here } }
The public static void main(String[] args)
block is said to be nested inside the public class VirtualZoo
block. Indentation has been used to aid readability and is strongly recommended.
To see the application do something, change the comment line above to the following, as marked in bold:
package virtualzoo; public class VirtualZoo { public static void main(String[] args) { System.out.println("Hello World"); } }
The System.out.println("Hello World");
statement sends the text you specify (i.e., "Hello World") to the Output window. To get it to run, you can use any of the following options:
- Click the green arrow icon in the toolbar (its hover text is Run Main Project); or
- Select the menu options Run | Run Main Project; or
- Right-click the
VirtualZoo
project node in the Projects window and select Run; or - Right-click the VirtualZoo.java source code node and select Run.
If you receive error notifications when entering the above or trying to run it, check that you have entered the statement exactly as specified – common mistakes include not using the correct capitalisation or forgetting to include a semi-colon at the end of the statement.
Whichever method you choose run it, NetBeans automatically compiles the source code for you, and you should see the output appear in the bottom section of NetBeans known as the Output window:
Later, you will modify the code so that instead of it sending "Hello World" to the Output window it launches the application you will be developing in this course.
In the next lesson you will see how to define a Java class to model an animal.