Beginning the Program
NetBeans groups related programs together into a project. If you don’t have the Minecraft project open, here’s how to retrieve it:
- Select File, Open Project. A file dialog appears.
- Find and select the folder where you installed the Bukkit server (if necessary).
- Open that folder.
- Select Minecraft and click Open Project.
The Minecraft 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 the project contains.
To add a new Java program to the currently open project, select File, New File. The New File Wizard opens, as shown in Figure 4.2.
FIGURE 4.2 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, select 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 Splash.
- In the Package field, enter com.javaminecraft.
- Click Finish.
So you can begin working right away on your program, an empty file named Splash.java opens in the source code editor. Using the editor, begin your Java programming career by entering each line from Listing 4.1. These statements are called the program’s source code.
LISTING 4.1 The Splash Program
1:package
com.javaminecraft; 2: 3:class
Splash { 4:public static void
main(String[] arguments) { 5:// My first Java program goes here
6: } 7: }
Be 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, select File, Save to save the file.
At this point, Splash.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 Splash 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, aside from the sample Minecraft mod you wrote and tested in Chapter 3, “Create a Minecraft Mod.”
The class Statement
The first line of the program is the following:
package
com.javaminecraft;
A package is a way to group Java programs together. This line tells the computer to make com.javaminecraft the package name of the program.
After a blank line, the third line is this:
class
Splash {
Translated into English, it means, “Computer, give my Java program the name Splash.”
As you might recall from Chapter 3, each instruction you give a computer is called a statement. The class keyword 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 Splash matches the document’s filename, Splash.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. Mods are an exception because they are run by the Bukkit server and cannot be run directly. When a player types a command that a mod supports, the server runs that mod.
Some other 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.
The Java programs you write during the next 13 chapters use main as their starting point. That’s because you run them directly on your computer. Mods, applets, apps, and servlets are run indirectly by another program or device.
To differentiate programs with main from these other types, they are called applications.
Those Squiggly Bracket Marks
In the Splash 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 4.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 Splash 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.