Components of a Class
Identifying the Components of a Class
Classes are the blueprints that you create to define the objects in a program. A desktop application usually consists of one object, often called the controller object, main object, or test object that is the starting point for your program.
The object interaction graphic displayed depicts some of the objects that might exist in the order entry program for DirectClothing, Inc. An OrderEntry object might interact with one or more Window objects, Customer objects, Order objects, while your program runs.
General Purpose and Unique Classes
Every object in the object interaction graphic displayed on the screen is an instance of a blueprint or class. For example, all Window objects are instances of Window classes. Some classes, such as the Window class, which is used for creating Graphical User Interface (GUI) windows, are general-purpose classes and are provided to you as part of the Java technology application programming interface (API). Other classes, such as the Shirt class, might be unique to your particular program, so you must create them yourself.
Structuring Classes
Classes are composed of the Java technology code necessary to instantiate objects, such as Shirt objects. The four different sections of a Java class are the class declaration, attribute variable declarations and initializations, methods, and comments. All sections except the class declaration are optional.
A class does not have to contain methods or attributes.
Class Structure and Source File
The programming code for a class is contained within a text file that must adhere to a certain structure. Java technology programming code you write for each class is in its own text file or source code file. In the Java programming language, source code file names must match the public class name in the source code file and must have a .java extension. For example, the Shirt class must be saved in a file called Shirt.java. The code displayed depicts a Shirt class modelled during “Designing Classes”. The Shirt class has several attribute variables and one method for printing the values of the variables.
The quantityInStock attribute would not appear in a catalogue but would be used to track the quantity of a particular shirt available in the warehouse.
Class Declaration
You must declare a class for each class you designed for the problem domain. For each class, you must write a class declaration.
The syntax for declaring a class is displayed. It contains three elements: modifiers, class, and class_identifier For example, the class declaration public class Shirt has the class modifier public followed by the class keyword. The class name is Shirt.
modifiers
[modifiers] public determines the accessibility that other classes have to this class. The [modifiers] element is optional. This is indicated by the square brackets. The keyword public is an access modifier. The other modifiers is private, and protected.
class
The class keyword tells the compiler that the code block is a class declaration. Keywords are words that are reserved by the Java programming language for certain constructs.
class_identifier
The class_identifier is the name given to the class. Class names should be nouns, in mixed case with the first letter of each word capitalized, for example, MyClass. In addition, class names should contain whole words. Avoid acronyms and abbreviations unless the abbreviation is much more widely used than the long form, such as JVM, which is used for Java Virtual Machine, or UML, which is used for Unified Modelling Language.
Variable Declarations and Assignments
In a Java class file, the attribute variable declarations and assignments block comes after the class declaration. This block follows the first open curly brace ({). Generally, all the attribute variables for the class are set up after this brace.
For example, the Shirt class code example displayed contains five attribute variable declarations, one for a shirt ID, one for a description, one for a colour code, one for price, and one for a quantity in stock.
Lines in the program that perform a task, such as printing or declaring variables, have a semicolon at the end. Lines in the program that represent the start of a block of code, which includes one or more code lines combined to accomplish a particular task, such as a class or method, start with an open brace ({) and end with a closed brace (}).
Comments
You should put comments in every class that you create to make it easier to determine what the program is doing. This is particularly important in longer programs developed by large teams where several programmers need to read the code. Commenting also helps with the maintenance of a program when new programmers need to determine what the code is doing.
The two main styles of comments that you can use are single-line comments and traditional comments.
There is a third type of comment called a documentation comment. Documentation comments must begin with a forward slash and two asterisks (/**) and end with an asterisk and a forward slash */. Some traditional comments can also be valid documentation comments.
Single-Line Comments
Single line comments are indicated by two slashes //. A // marker tells the compiler to ignore everything to the end of the current line. Notice the single-line comments in the Shirt class code displayed. Many programmers also make their programs easier to read by using single-line comments to comment the first and last line of every class. For example, the Shirt class contains an end-of-line comment to indicate the end of the display method. In long programs, it can be very difficult to find the ending braces of the class, commenting the structure that each ending brace belongs to makes reading and fixing errors much easier.
Traditional Comments
Traditional comments begin with a /* character combination. This combination tells the compiler to ignore everything on all lines up to, and including, a comment termination marker */. Programmers often use traditional comments to provide details for a large block of code.
Methods
Methods follow the attribute variable declarations in a class. The syntax for methods is. It contains the elements modifiers, return_type, method_identifier, arguments, and method_code_block.
The Shirt class example contains one method, the displayShirtInformation()method, which displays the values for the attributes of a shirt.
[modifiers]
The [modifiers] represent several unique Java technology keywords that modify the way methods are accessed. Modifiers are optional. This is indicated by the square brackets.
return_type
The return_type indicates the type of value, if any, that the method returns. If the method returns a value, the type of the value must be declared. Returned values can be used by the calling method. Any method can return at most one value. If the method returns nothing, the keyword void must be used as the return_type.
method_identifier
method_identifier is the name of the method.
[arguments]
The [arguments] represent a list of variables whose values are passed to the method for use by the method. Arguments are optional, as indicated by the square brackets because methods are not required to accept arguments. Also note that the parentheses are not optional. A method that does not accept arguments is declared with an empty set of parentheses.
method_code_block
The method_code_block is a sequence of statements that the method performs. A wide variety of tasks can take place in the code block or body of the method.
Creating and Using a Test Class
Every class must be tested according the Software Product Life Cycle process. The code displayed is an example of a test class for the Shirt class. A test class is generally named in such a way that it can be recognized as the test class for a particular class.
Each test class name generally consists of the name of the class you are testing, followed by the word "Test." For example, the class designed to test the Shirt class is called ShirtTest.
Test classes have two distinct tasks to perform. These are providing a starting point, called the main method, for your program and creating an object instance of your class and testing its methods.
The main Method
The main method is a special method that the Java Virtual Machine (JVM) recognizes as the starting point for every Java technology program that runs from the command line or from a prompt. Any program that you want to run from the command line or a prompt must have a main method. The syntax for the main method is displayed.
Many of the Java technology classes that engineers create do not run within an operating system. Applets run within a web browser and have their own unique starting method.
Features of the main Method
The main method adheres to the syntax for all methods described earlier and contains two required modifiers, public and static.
This method does not return any values, so it has a return type of void, has a method identifier name of main, and accepts zero or more objects of type String (String args[]). This syntax allows you to type in values on the command line to be used by your program while it is running.
The name of the array (args) that the main method accepts can be changed. The syntax public static void main (String[] args) is also acceptable. Every class can contain any number of overloaded main methods, including public static void main (String [] args). Only one method will be called directly by the command java classname(Run).
Compiling and Executing a Program
NB! This section explains the compiling and executing a program using the Java Development Kit (JDK). Most of the IDE’s (Integrated Development Environment) like, JGrasp, Netbeans and Eclipse have got a compile and run commands to perform these operations. The compile and run commands from these IDE’s are utilising the compile and run processes of the JDK in the background.
Compiling a Program
The basic Java technology class called Shirt and a test class called ShirtTest together make up a Java technology program. Next, you compile, execute, and test your program.
Compiling converts the class files you write into bytecode that can be executed by a JVM.
Remember the rules for naming your Java source files. If a source file contains a public class, the source file must use the same name as the public class, with a .java extension. For example, the class Shirt must be saved in a file called Shirt.java.
Your system must be configured properly before you can compile and execute any Java technology programs.
Steps to Compile a Program
To compile the Shirt and ShirtTest source code files, you would perform certain steps:
· First, go to the directory where the source code files are stored.
· Next, enter the command javac filename for each .java file that you want to compile. For example, javac Shirt.java.
Notice that after the compilation has finished, and assuming no compilation errors have occurred, a new file called classname.class is created in the directory for each source code file compiled. If you compile a class which references other objects, the classes for those objects are also compiled if they have not been compiled already. For example, on compiling ShirtTest.java file, which references a Shirt object, you also have a Shirt.class and ShirtTest.class file.
Executing a Program
When you have successfully compiled your source code files, you can execute and test them using the JVM. To execute and test your program, you need to perform certain steps:
First, go to the directory where the class files are stored.
Next, enter the command java classname to run the class file that contains the main method. For example, java ShirtTest.
The ShirtTest class creates an instance of the Shirt object, using the Shirt class. All Shirt objects have one method, the displayShirtInformation()method, which prints the values of their attribute variables as depicted in the graphic displayed on the screen.
Executing and testing are often synonymous and represent the fourth stage in the PLC.
Debugging Tips
Generally, there is at least one error in any code. Debugging is the term used for fixing errors or bugs in your code. You can use some tips while debugging programs. Error messages state the line number where the error occurs. However, the line that is provided might not always be the actual source of the error, so you might have to check the lines of code before or after the line number that is provided. Make sure that there is a semicolon at the end of every statement. Check that there is an even number of braces, a closing brace for each opening brace. Ensure that there is consistent indentation in your program. Even though your programs will run without indenting different types of code, indenting makes the code easier to read and debug, which makes it easier to avoid mistakes while you are writing.
http://jessicajava.blogspot.com/search/label/Test%20Class
http://jessicajava.blogspot.com/search/label/Class%20Components
http://jessicajava.blogspot.com/search/label/Class%20Declaration
http://jessicajava.blogspot.com/search/label/Compilation
http://jessicajava.blogspot.com/search/label/Main%20Method%20Declaration