Collection Contents Index Java in the database Q & A The Sybase runtime environment for Java pdf/chap16.pdf

User's Guide
   PART 3. Java in the Database
     CHAPTER 16. Welcome to Java in the Database       

A Java seminar


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.

Java examples directory    
All complete classes used as examples in this document are located in the Java examples directory, jxmp, which is a sub directory of your Adaptive Server Anywhere installation directory.

Each Java class example is represented by two files: the Java class declaration and the compiled version. The compiled version of the Java class examples can be immediately installed to a database without modification.

Top of page  Object oriented and procedural languages

If you are more familiar with procedural languages such as C, or the SQL stored procedure language, than object-oriented 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 could be looked at as just a collection of procedures and variables that have been grouped together because they all relate to a specific, identifiable category.

However the manner in which a class gets used 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 pre-defined course of execution.

In object-oriented languages such as Java, a class is used like a template: a definition of potential program execution. Multiple copies of the 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 could be acted on or executed independently of any other class loaded into memory.

A class that is loaded into memory for execution it 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.

Top of page  Understanding the Java class

One way of understanding the concept of a class is to view it as an entity, an abstract representation of a thing.

An Invoice class, for example, could be designed to mimic a paper invoice such as those used every day in business operations. A paper invoice contains certain information, like line-item details, who is being invoiced, the date of the invoice, the payment amount and when payment is due. This is information that could also be contained in an instance of an Invoice class.

In addition to just holding data, a class is capable of calculations and logical operations. The Invoice class, for example, could be designed to calculate the tax on a list of line items, and add it to the sub total to produce a final total. This could be done for every Invoice object 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 over due or partially paid.

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

Example 

The following Java code 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.

A note about compiling Java classes    
Compiling a Java class declaration does not alter the class declaration: it 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 used, 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.

In fact, no Java code exists outside class declarations. Even the Java procedure that a Java interpreter runs automatically to create and manages other objects — the main method that is often the start of your application — is itself located within a class declaration.

Top of page  Java classes and objects

A class is the template for what can be done, just as an invoice form is only the template used to generate an invoice. A class defines what an object is capable of doing just as an invoice form defines what information the invoice should contain.

Information is not held in the 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 is the incarnation of the class that 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 invoices, with each invoice separate and distinct from the other in its details, many objects can be generated from one class.

Methods and fields 

A method is the part of the class that does something — a function that performs a calculation or interacts with other objects — on behalf of the class. 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 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 

The following declaration of the class Invoice has four fields, corresponding to information that might be contained on two line items on an invoice.

To declare a field in a class, state its type and then its 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;

}

Top of page  Adding methods

Another possible modification to the Invoice class is to include a method. A method is declared 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 sub total of the first and second line item cost. This sub total is then multiplied by 15 per cent (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.

Top of page  Instance methods and class methods

Most methods are used in association with an instance of a class. A totalSum method in the Invoice class could calculate and add the tax, and return the sum of all costs, but would only be useful if it is called in conjunction with an Invoice object, one that had 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 pre-determined 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 named rateOfTaxation returns the rate of taxation used by the class to calculate the total sum of the invoice.

The advantage of making the rateOfTaxation method 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.

Making it a method, as opposed to a field, allows the application developer to change how the rate is calculated without adversely effecting any objects, applications or procedures that use its return value. Future versions of Invoice could make the return value of the rateOfTaxation class method based on a more complicated calculation 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;
    }
}

Top of page  A Java glossary

The following items outline some of the details regarding Java classes. It is by no means an exhaustive source of knowledge about the Java language but may aid in the use of Java classes in Adaptive Server Anywhere.

For Info     For a thorough examination of the Java language, see the online book Thinking in Java, by Bruce Eckel, included with Adaptive Server Anywhere in the file jxmp\Tjava.pdf.

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 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.

Methods can also be declared private or public. This has the same effect as if it were a field being declared private or public.

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. Public fields and methods represent everything the external users of the class (including other classes) need to know or are allowed to know.

Packages and the protected modifier 

Protected fields or methods are accessible only to the following:

A package is a grouping of classes that share a common purpose or category. One member of a package has 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, which can be made available using the import statement. The following Java statement imports the utility library from the Java API:

import java.util.*

For Info    Packages are typically held in 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 which arguments were used in the attempt 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 their own finalize method for clean up operations when an object is being discarded, but there is no guarantee that this method will get called.

An object that has no references to it will be removed automatically by a "garbage collection" process. This does not apply to objects stored as values in a table.

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 only from 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 be compiled.

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, it is up to the class to determine how it is implemented.

Top of page  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 body of the method where the exception occurred, then 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.

In Adaptive Server Anywhere, if a SQL statement calls a Java method, and an unhandled exception is thrown, a SQL error is generated.

Error types in Java 

All errors in Java are derived from two types of error classes: Exception and Error. Usually, Exception-based errors are handled by error handling code in your method body. Error type errors are reserved for internal errors and resource exhaustion errors inside the Java run-time 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 is code that will execute if an error is generated (thrown) during the execution of a try block.

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

Exception class errors are divided into two types: those that are runtime exceptions and those that are not runtime exceptions.

Errors generated by the runtime system are known as implicit exceptions, in that 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 considered to be 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.

Essentially, explicit exceptions must be dealt with explicitly. 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 of 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.

Top of page  

Collection Contents Index Java in the database Q & A The Sybase runtime environment for Java pdf/chap16.pdf