Database Reference
In-Depth Information
Beginning Java Syntax
Let's put a couple Java code files on display here so that we can talk about some specific programming
concerns with concrete references. For discussion only, we will have two .java files exist in a directory
named
mypkg
, and
mypkg
is in a directory named
javadev
. The directory structure and filenames look like
this:
javadev/
mypkg/
MyApp.java
MyRef.java
Note
You will find the files in the
javadev/mypkg
directory.
Consider the following code listings for our two Java files, shown in Listings 3-1 and 3-2. Note that
these files don't do anything, but they are valid Java code.
Listing 3-1.
MyApp.java
package mypkg;
import oracle.sql.ARRAY;
public class MyApp {
ARRAY myArray;
MyRef myRef;
}
Listing 3-2.
MyRef.java
package mypkg;
public class MyRef {
}
We will cover several aspects of the syntax of Java code here, but we will be introducing the bulk of
the syntax a bit later. Notice that both of the Java code files begin with the declaration of their package.
Looking at
MyRef.java
, we see the simple declaration of the class with the modifier
public
. The code for a
class is enclosed in a pair of curly brackets ({}). Each .java file must have only one top-level class
declaration, and the top-level class must be named identical to the file (in this case, MyRef).
Note
Often these top-level classes are declared
public
, but they don't have to be. They cannot be private or
protected, but they can be default, or package accessible with no modifier.
Another observation at this point is in the
MyApp.java
file. First, we have an
import
statement,
import
oracle.sql.ARRAY
. A statement like this is required every time you use a class from a different package. If