Chapter 7 PowerDynamo and Java


An introduction to Java

This section introduces key Java concepts. After reading this section you should be able to examine Java code, such as a simple class definition or the invocation of a method, and understand what is taking place.

Object-oriented and procedural languages

This section explains some of the key similarities and differences between procedural and object-oriented languages.

Java is based on classes

The main structural unit of code in Java is a class. A Java class is a collection of procedures and variables that have been grouped together because they all relate to a specific, identifiable category.

The manner in which classes get used is what sets object oriented languages apart from procedural languages. When an application written in a procedural language is executed, it is typically loaded into memory once and takes the user down a predefined course of execution.

In object-oriented languages such as Java, classes are used like templates, in other words, a definition of potential program execution. Multiple copies of a class can be created and loaded dynamically, as needed, with each instance of the class capable of containing its own data, values, and course of execution. Each loaded class can be acted on or executed independently of any other class loaded into memory.

A class that is loaded into memory for execution is said to have been instantiated. An instantiated class is called an object - it is an application derived from the class that is prepared to hold unique values or have its methods executed in a manner independent of other class instances.

For example, you could design an Invoice class to mimic a paper invoice and include certain information, like line-item details, who is being invoiced, the date of the invoice, the payment amount, and when payment is due. This information is contained in an instance of an Invoice class.

In addition to just holding data, a class is capable of calculations and logical operations. For example, you could also define the Invoice class to calculate the tax on a list of line items, and add it to the subtotal to produce a final total. You could define every Invoice object to do this without user intervention.

Such a class could also ensure all essential pieces of information are added to the invoice and even indicate when payment is overdue or partially paid.

A class combines the ability to hold information and perform computational operations.

Example

The code below declares a class called Invoice. This class declaration would be stored in a file named Invoice.java. It could then be compiled into a Java class, using a Java compiler.

Note  

A note about compiling Java classes
Compiling a Java class declaration creates a new file with the same name but a different extension. Compiling Invoice.java creates a file called Invoice.class that could be used in a Java application and executed by a Java VM.

The Sun JDK tool for compiling class declarations is javac.exe.

public class Invoice {
// So far, this class does nothing and knows nothing
}

The class keyword is followed by the name of the class. There is an opening and closing brace: everything declared between the braces, such as fields and methods, becomes a part of the class.

No Java code exists outside class declarations. Even the Java procedure that a Java interpreter runs automatically to create and manage other objects -- the main method that is often the start of your application--is located within a class declaration.

Java classes and objects

A class is the template for what can be done-it defines what an object is capable of doing just as an invoice form defines the information the invoice should contain.

Information is not held in a class. An object is created based on the class and the object is used to hold data or perform calculations or other operations.

An object is said to be of type JavaClass, where JavaClass is the name of the class upon which the object is based. An Invoice object is an instance of the Invoice class. The class defines what the object is capable of but the object gives the class meaning and usefulness.

This is similar to the invoice example. The invoice form defines what all invoices based on that form can accomplish. There is one form and zero or many invoices based on the form. The form contains the definition but the invoice does the work.

Similarly it is the Invoice object that gets created, stores information, is stored, retrieved, edited, updated, and so on.

Just as one invoice template is used to create many separate and distinct invoices, many objects can be generated from one class.

Methods and fields

A method is the part of the class that performs a calculation or interacts with other objects. Methods can accept arguments, and return a value to the calling function. If no return value is needed, a method can return void. Classes can have any number of methods.

A field is the part of a class that ends up holding information. When an object of type JavaClass is created, the fields in JavaClass are available to be passed values unique to that object.

Examples

To declare a field in a class, state its type and name, followed by a semicolon. Such a variable is a field if it is declared in the body of the class and not within a method. Declaring a variable within a method makes it a part of the method, not the class.

public class Invoice {

// Fields are things an Invoice object knows
public String lineItem1Description;
public int lineItem1Cost;

public String lineItem2Description;
public int lineItem2Cost;

}

Adding methods

You might want to include a method in the Invoice class. Declare a method by stating its return type, its name, and what parameters it takes (in this case, none). Like a class declaration, the method uses an opening and closing brace to identify the body of the method where the code goes.

public class Invoice {

// Fields
public String lineItem1Description;
public double lineItem1Cost;

public String lineItem2Description;
public double lineItem2Cost;

// A method
public double totalSum() {
double runningsum;

runningsum = lineItem1Cost + lineItem2Cost;
runningsum = runningsum * 1.15;

return runningsum;
}
}

Within the body of the totalSum method, a variable named runningsum is declared. This is used first to hold the subtotal of the first and second line item cost. This subtotal is then multiplied by the rate of taxation to determine the total sum.

The local variable (as it is known within the method body) is then returned to the calling function. When the totalSum method is invoked, it returns the sum of the two line item cost fields plus the cost of tax on those two items.

Instance methods and class methods

Most methods are used with an instance of a class. For example, a totalSum method in the Invoice class can calculate and add the tax, and return the sum of all costs, but is useful only if called in conjunction with an Invoice object that has values for its line item costs. The calculation can only be performed for an object, since the object, not the class, contains the line items of the invoice. The class only defines the capability of the object to have line items. Only the object has the data needed to perform such a calculation.

Java methods are divided into two categories:

The parseInt method of the java.lang.Integer class, which is supplied with Adaptive Server Anywhere, is one example of a class method. When given a string argument, the parseInt method returns the integer version of the string.

For example given the string value "1," the parseInt method returns 1, the integer value, without requiring an instance of the java.lang.Integer class to first be created as illustrated by this Java code fragment.

String num = "1";
int i = java.lang.Integer.parseInt( num );

Fields can also be declared using the static Java keyword, which makes them into class fields. A static class variable is like a global variable in procedural languages, except that its value is predetermined and cannot be changed. A field whose name is all in capital letters is often a static variable (class field).

Examples

The following version of the Invoice class now includes both an instance method and a class method. The class method rateOfTaxation returns the rate of taxation used by the class to calculate the total sum of the invoice.

The advantage of making rateOfTaxation a class method (as opposed to an instance method or field) is that other classes and procedures can use the value returned by this method without having to create an instance of the class first. Only the name of the class and method is required to return the rate of taxation used by this class.

It allows the application developer to change how the rate is calculated without adversely effecting any objects, applications, or procedures that use its return value. rateOfTaxation can be changed without affecting other methods that use its return value.

public class Invoice {

// Fields
public String lineItem1Description;
public double lineItem1Cost;

public String lineItem2Description;
public double lineItem2Cost;

// An instance method
public double totalSum() {
double runningsum;
double taxfactor = 1 + Invoice.rateOfTaxation();

runningsum = lineItem1Cost + lineItem2Cost;
runningsum = runningsum * taxfactor;

return runningsum;
}

// A class method
public static double rateOfTaxation() {
double rate;
rate = .15;

return rate;
}
}

A Java glossary

The following items outline some details regarding Java classes that may aid in the use of Java classes in PowerDynamo.

Public versus private

The visibility of a field, method, or class to other Java objects and operations is determined by what is known as an access modifier -- essentially the public , private, or protected keyword used in front of any declaration.

Fields and methods can be declared private or public, meaning, respectively, their values are available to code within the object, or to code/classes/objects both inside and outside the object.

Fields or methods declared as private cannot be manipulated or accessed by methods outside the class. Public fields or methods can be directly accessed by other classes and methods.

Packages and the protected modifier

Protected fields or methods are accessible only:

A package is a grouping of classes that share a common purpose or category. Members of a package have special privileges to access data and methods in other members of the package, hence the protected access modifier.

A package is the Java equivalent of a library. It is a collection of classes, that can be made available using the import statement. The following Java statement imports the utility library from the Java API:

import java.util.*

Packages are typically held in Java archive (Jar) files, which have the extension .jar or .zip.

Constructors

A constructor is a special method of a Java class that creates an instance of the class and returns a reference to the newly-created Java object.

Classes can define their own constructors, including multiple, overriding constructors. Which constructor is used is determined by the arguments used to create the object. When the type, number, and order of arguments used to create an instance of the class match one of the class's constructors, that constructor is used to create the object.

Destructors

There is no such thing as a destructor method in Java (as there is in C++). Java classes can define a finalize method for clean-up operations when an object is discarded, but there is no guarantee that this method will get called.

An object that has no references to is automatically removed by a "garbage collection" process.

Other C++ differences

Everything related to a class is contained within the boundaries of the class declaration, including all methods and fields.

Classes can inherit from only one class. Java uses interfaces instead of multiple-inheritance. A class can implement multiple interfaces. Each interface defines a set of methods and method profiles that must be implemented by the class in order for the class to compile.

An interface is similar to an abstract class; it defines what methods and static fields the class must declare. The implementation of the methods and fields declared in an interface is located within the class that uses the interface. The interface defines what the class must declare, and it is up to the class to determine how it is implemented.

Java error handling

Java error handling code is separate from the code for normal processing.

When an error occurs, an exception object representing the error is generated. This is called throwing an exception. A thrown exception will terminate a Java program unless it is caught and handled properly at some level of the application.

Both Java API classes and custom-created classes can throw exceptions. In fact, users can create their own exceptions classes, which can be thrown by their own custom-created classes.

If there is no exception handler in the method where the exception occurred, the search for an exception handler continues up the call stack. If the top of the call stack is reached and no exception handler has been found, the default exception handler of the Java interpreter running the application is called and the program terminates.

Error types in Java

All errors in Java are derived from two types of error classes: Exception and Error . Usually, exception errors are handled by error-handling code in your method body. Error type errors are reserved for internal and resource exhaustion errors inside the Java runtime system.

Exception class errors are thrown and caught. Exception handling code is characterized by try , catch , and finally code blocks.

A try block executes code that may generate an error. A catch block will execute if an error is generated (thrown) during the execution of a try block.

A finally block defines code executes regardless of whether an error was generated and caught and is typically used for clean-up operations. It is used for code that, under no circumstances, can be omitted.

Exception errors are divided into runtime exceptions and those that are not runtime exceptions.

Errors generated by the runtime system are known as implicit exceptions, because they do not have to be explicitly handled as part of every class or method declaration.

For example, an array out of bounds exception can occur whenever an array is used, but the error does not have to be part of the declaration of the class or method that uses the array.

All other exceptions are explicit. If the method being invoked can throw an error, it must be explicitly caught by the class using the exception throwing method, or this class must explicitly throw the error itself by identifying the exception it may generate in its class declaration.

A method must declare all the explicit errors it throws, or catch all the explicit errors that may be potentially thrown.

Non-runtime exceptions are checked at compile time. Runtime exceptions are usually caused by errors in programming. Java catches many such errors during compilation, before the code is run.

Every Java method is given an alternative path of execution so that all Java methods complete, even if they are unable to complete normally. If the type of error that is thrown is not caught, it's passed to the next code block or method in the stack.

 


Copyright © 1999 Sybase, Inc. All rights reserved.