Packages


Java Source File Structure

This section provides a brief introduction to the structure of a typical Java class, source file, and application. Example 1.1 gives the source code for a Java class named SampleClass.

Example 1.1: SampleClass.java

image from book
 image from book 
image from book

This source code appears in a file named image from book SampleClass.java. You can create the image from book SampleClass.java file with a text editor such as NotePad, TextEdit, or with the text editor that comes built-in with your chosen integrated development environment (IDE). There can be many class definitions in one Java source file, but only one public class. The name of the file must match the name of the public class with the .java suffix added.

Line 1 gives a package directive stating that SampleClass belongs to the com.pulpfreepress.jfa.chapter1 package. The package directive, if one exists, must be the first non-comment line in a Java source file.

Line 3 is an import directive. An import directive does not physically import anything into the Java source file. Rather, it allows programmers to use shortcut names for components belonging to included package names.

The SampleClass class declaration starts on line 5 and ends on line 33. Everything between the opening brace { on line 5 and the closing brace } on line 33 is in the body of the class definition.

A multi-line comment appears on lines 6 through 8. Java allows three types of comments, and each type is discussed in greater detail later in this chapter.

Class and instance field declarations appear on lines 9 through 11. The class-wide constant named CONST_VAL is declared on line 9 using the keywords static and final. A class-wide variable named class_variable is declared on line 10. Notice the difference between the names CONST_VAL and class_variable. CONST_VAL is in uppercase letters with an underscore character separating each word. It is generally considered to be good programming practice to put constant identifiers in uppercase letters to distinguish them from variables which appear in lowercase.

An instance variable named instance_variable is declared on line 11. Every instance of SampleClass will have its own copy of instance_variable, but share a copy of class_variable.

A special method named SampleClass() is defined on line 13. The SampleClass() method is referred to as a constructor method. The constructor method is special because it bears the same name as the class in which it appears and has no return type. In this case, the SampleClass() constructor method prints a simple message to the console when instances of SampleClass are created.

A static method named setClassVariable() is defined beginning on line 17. The setClassVariable() method takes an integer argument and uses it to set the value of class_variable. Static methods are known as class methods, whereas non-static methods are referred to as instance methods. I will show you the difference between these two method types later in this chapter.

Another static method named getClassVariable() is declared and defined on line 21. The getClassVariable() methods takes no arguments and returns an integer value. In this case, the value returned is the value of class_variable.

The last two methods, setInstanceVariable() and getInstanceVariable() are non-static instance methods. They work like the previous methods but only on instances of SampleClass.

SampleClass In Action

Although SampleClass is a complete Java class, it cannot be executed by the Java Virtual Machine. A special type of class known as an application must first be created. Example 1.2 gives the source code for a class named ApplicationClass.

Example 1.2: ApplicationClass.java

image from book
 image from book 
image from book

image from book ApplicationClass.java is similar to image from book SampleClass.java in that it contains a package-declaration statement as the first non-comment line. It also contains a class declaration for ApplicationClass, which is the same name as the Java source file. The primary difference between SampleClass and ApplicationClass is that ApplicationClass has a special method named main(). A Java class that contains a main() method is an application.

The main() method is declared on line 5. It takes an array of strings as an argument although in this example this feature is not used.

An instance of SampleClass named sc is declared and created on line 6. Then, from lines 7 through 14, both the SampleClass and the instance of SampleClass, sc, are used in various ways to illustrate how to invoke each type of method (static and instance).

To test these classes, they first must be compiled with the javac compiler and then run using the java command. The results of running example 1.2 are shown in figure 1-1. For detailed discussions regarding how to compile these Java classes using various development environments refer to chapter 2.

image from book
Figure 1-2: Results of Running Example 1.2

General Rules For Creating Java Source Files

There are a few rules you must follow when creating Java source files, and you were introduced to a few of them above.

First, Java source files contain plain text (ASCII), so use a plain-text editor to create the source file. (You could use a word processing program like Microsoft Word to create a Java source file but you would have to save the file as plain text.) The characters appearing in a source file can be UNICODE characters. But since most text editors only provide support for ASCII characters, Java programs can be written in ASCII. Escape sequences can be used to include ASCII representations of UNICODE characters if they are required in a program.

Second, a Java source file can contain an optional package directive. If a package directive appears in a source file it must be the first non-comment line in that source file.

Third, a Java source file can contain zero or more import directives. Import directives do not physically import anything into a Java source file; rather, they provide component-name shortcuts. For example, to use a Swing component in a Java program you could do one of two things: 1) you could include the import javax.swing.*; import directive in your source file and then use individual component names such as JButton or JTextField in your program as necessary, or, 2) you could leave out the import directive and use the fully qualified name for each Swing component you need, such as javax.swing.JButton or javax.swing.JTextField .

Fourth, a Java source file can contain any number of top-level class or interface definitions, but there can only be one public top-level class or interface in the source file. The name of the source file must match the name of this public class or interface with the addition of the .java file extension.

Finally, each top-level class can contain any number of publicly declared inner or nested class definitions. You will soon learn, however, it is not always a good idea to use nested or inner classes. Table 1-1 summarizes these Java source-file rules:

Table 1-1: Java Source File Rules Summary

Issue

Rule

1. Java source file creation and character composition

The UNICODE character set can be used to create a Java source file, however, since most text editors support only the ASCII character set, source files are created in ASCII and UNICODE characters represented when necessary using ASCII escape sequences. A source file must end in a .java file extension.

2. Package directive

Optional If a package directive appears in a source file it must be the first non-comment line in the file.

3. Import directives

Optional There can be zero or more import directives. Import directives allow you to use unqualified component names in you programs.

4. Top-level class and interface definitions

There can be one or more top-level class or interface definitions in a source file, but, there can only be one public class or interface in a source file. The filename must match the name of the public class or interface. A source file must end with a .java file extension.

5. Nested and inner classes

A top-level class declaration can contain any number of nested or inner class definitions. (I recommend avoiding nested and inner classes whenever possible.)

Rule-of-Thumb: One Class Per File

Although Java allows multiple classes and interfaces to be defined in a single source file, it s helpful to limit the number of classes or interfaces to one per file. One class per file helps you manage a project s physical complexity because finding your source files when you need them is easy when the name of the file reflects the name of the class or interface it contains.

Avoid Anonymous, Nested, And Inner Classes

Java also allows anonymous, nested, and inner classes to be defined within a top-level class. Anonymous, nested, and inner class definitions add a significant amount of conceptual complexity if used willy-nilly. Their use can also complicate the management of physical complexity, because they can be hard to locate when necessary.

Create A Separate Main Application File

A Java class can contain a main() method. The presence of a main() method changes an otherwise ordinary class into an application that can be run by the Java Virtual Machine. Novice students, when writing their first Java programs, are often confused by the presence of the main() method. Therefore, when writing Java programs, I recommend you create a small class whose only purpose is to host the main() method and serve as the entry point into your Java program. Refer to examples 1.1 and 1.2 for examples of this approach.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net