Beginning the Program
NetBeans groups related programs together into a project. If you don’t have the Java24 project open, here’s how to retrieve it:
- Choose File, Open Project. A file dialog appears.
- Find and select the NetBeansProjects folder (if necessary).
- Choose Java24 and click Open Project.
The Java24 project appears in the Projects pane next to a coffee cup icon and a + sign that can be expanded to see the files and folders that the project contains.
To add a new Java program to the currently open project, choose File, New File. The New File Wizard opens, as shown in Figure 2.1.
FIGURE 2.1 The New File Wizard.
The Categories pane lists the different kinds of Java programs you can create. Click the Java folder in this pane to see the file types that belong to this category. For this first project, choose the Empty Java File type and click Next.
A New Empty Java File dialog opens. Follow these steps to begin writing the program:
- In the Class Name field, enter Saluton.
- In the Package field, enter com.java24hours.
- Click Finish.
So you can begin working right away on your program, an empty file named Saluton.java opens in the source code editor. Using the editor, begin your Java programming career by entering each line from Listing 2.1. These statements are called the program’s source code.
LISTING 2.1 The Saluton Program
1:package
com.java24hours; 2: 3:class
Saluton { 4:public static void
main(String[] arguments) { 5:// My first Java program goes here
6: } 7: }
Make sure to capitalize everything exactly as shown, and use your spacebar or Tab key to insert the blank spaces in front of Lines 4–6. When you’re done, choose File, Save to save the file.
At this point, Saluton.java contains the bare-bones form of a Java program. You will create many programs that start exactly like this one, except for the word Saluton on Line 3. This word represents the name of your program and changes with each program you write. Line 5 should make sense to you, because it’s a sentence in actual English. The rest is probably new to you.
The class Statement
The first line of the program is the following:
package
com.java24hours;
A package is a way to group Java programs together. This line tells the computer to make com.java24hours the package name of the program.
After a blank line, the third line is this:
class
Saluton {
Translated into English, it means, “Computer, give my Java program the name Saluton.”
As you might recall from Hour 1, each instruction you give a computer is called a statement. The class statement is the way you give your computer program a name. It’s also used to determine other things about the program, as you will see later. The significance of the term class is that Java programs also are called classes.
In this example, the program name Saluton matches the document’s filename, Saluton.java. A Java program must have a name that matches the first part of its filename and should be capitalized the same way.
If the program name doesn’t match the filename, you get an error when you try to compile some Java programs, depending on how the class statement is being used to configure the program.
What the main Statement Does
The next line of the program is the following:
public static void
main(String[] arguments) {
This line tells the computer, “The main part of the program begins here.” Java programs are organized into different sections, so there needs to be a way to identify the part of a program that is executed first when the program is run.
The main statement is the entry point to most Java programs. The exceptions are applets, programs that are run on a web page by a web browser; servlets, programs run by a web server; and apps, programs run by a mobile device.
Most programs you write during upcoming hours use main as their starting point. That’s because you run them directly on your computer. Applets, apps, and servlets are run indirectly by another program or device.
To differentiate them from these other types, the programs that you run directly are called applications.
Those Squiggly Bracket Marks
In the Saluton program, Lines 3, 4, 6, and 7 contain a squiggly bracket mark of some kind—either a { or a }. These brackets are a way to group lines of your program (in the same way that parentheses are used in a sentence to group words). Everything between the opening bracket { and the closing bracket } is part of the same group.
These groupings are called blocks. In Listing 2.1, the opening bracket on Line 3 is associated with the closing bracket on Line 7, which makes your entire program a block. You use brackets in this way to show the beginning and end of a program.
Blocks can be located inside other blocks (just as parentheses are used in this sentence (and a second set is used here)). The Saluton program has brackets on Line 4 and Line 6 that establish another block. This block begins with the main statement. The lines inside the main statement’s block will be run when the program begins.
The following statement is the only thing located inside the block:
// My first Java program goes here
This line is a placeholder. The // at the beginning of the line tells the computer to ignore this line because it was put in the program solely for the benefit of humans who are looking at the source code. Lines that serve this purpose are called comments.
Right now, you have written a complete Java program. It can be compiled, but if you run it, nothing happens. The reason is that you haven’t told the computer to do anything yet. The main statement block contains only a single comment, which is ignored by the computer. You must add some statements inside the opening and closing brackets of the main block.