Game Development Reference
In-Depth Information
Besides API level (the SDK you installed and are using), the highest-level construct
in the Java programming language is the
package
. A Java package uses the
package
keyword to declare the application's package at the top of your Java code. This must be
the first line of code declared, other than comments (see
Figure 3-1
; see also
Chapter
ate your package for you and will import other packages that you will need to use,
based on what you want to do in your application. In this case, these are JavaFX pack-
ages, so you can use the JavaFX new media engine.
As you may have ascertained from the name, a Java package collects all the Java
programming constructs. These include classes, interfaces, and methods that relate to
your application, so the invinciBagel package will contain all your code, as well as the
code that you imported to work with your code, to create, compile, and run the Invin-
ciBagel game.
A Java package is useful for organizing and containing all your own application
code, certainly, but it is even more useful for organizing and containing the SDK's
(API's) Java code, which you will use, along with your own Java programming logic,
to create your Java 8 applications. You can use any of the classes that are part of the
API that you are targeting by using the Java
import
keyword, which, in conjunction
with the package and class that you want to use, constitutes an
import statement
.
The import statement begins with the
import keyword
, followed by the
fully qual-
ified class name
, which is the
package name,
any
subpackage name,
and the
class
name
as a complete
naming reference path
(the full proper name for the class). A
semicolon terminates
an import statement. As you have already seen in
Figure 3-1
,
the import statement used to import the JavaFX
EventHandler
class from the
javafx.event
package should look just like this:
import
javafx.event.EventHandler;
The import statement tells the Java compiler that you will be using methods (or
constants) from the class that is referenced, using the import keyword, as well as which
package the class is stored in. If you use a class, method, or interface in your own Java
class, such as the
InvinciBagel
class (see
Figure 3-2
), and you have not
declared
the
class for use, using the import statement, the Java compiler will
throw
an error until
you add the required import statement at the top of the class (after the Java package de-
claration statement, and before the Java class declaration statement).