1153-1156

Previous Table of Contents Next

Page 1153

Keep in mind that none of these simple examples can exist outside a class method. Java is so object-oriented that it forces a certain degree of encapsulation in this way. Because all the work of a Java program is handled by objects, it is very important to understand how to define objects and instantiate instances of them. Each Java class has a constructor. (If you do not define a constructor, a default constructor is generated.) The constructor has the same name as the class and can be overloaded to accept different parameters.

Java classes do not need destructors because the virtual machine has a garbage collector that destroys objects that no longer are referenced. A class can define a finalize() method, however. This method is invoked by the garbage collector prior to destroying the object. This method can be implemented to ensure that any externally allocated resources are freed.

NOTE
Java makes no guarantees when garbage collection will occur or in what order objects will be collected. Therefore, Java makes no guarantee about when a finalizer() will be invoked, or in what order finalizers will be invoked. Java also does not guarantee that a finalizer() will be called.

Listing 52.1 uses interface and class definitions to illustrate many of the basic concepts of the Java object model and programming language that have been described in this chapter.

Listing 52.1. Interface and class definitions.

 package Calc; public interface CalcOps {     public final static int ADD = 1;     public final static int SUBTRACT = 2;     public final static int MULTIPLY = 3;     public final static int DIVIDE = 4;     public final static int MODULUS = 5;     public final static int SQUARE_ROOT = 6; } package Calc; public interface CalcHelper {     double Calc(double Val1, int OpCode, double Val2); } package Calc; public final class DumbCalc {     protected CalcHelper helper = null;     protected double CurrentVal = 0;     public DumbCalc() {}     public DumbCalc(double ValIn) {}     public DumbCalc(double ValIn,     CalcHelper helperIn) {         CurrentVal = ValIn;         helper = helperIn; 
 continues 

Page 1154

Listing 52.1. continued

 }     public synchronized double CalcResult(int    op,     double ValIn) throws java.lang.Exception {         switch (op) {             case CalcOps.ADD:                 CurrentVal += ValIn;                 break;             case CalcOps.SUBTRACT:                 CurrentVal -= ValIn;                 break;             case CalcOps.MULTIPLY:                 CurrentVal *= ValIn;                 break;             case CalcOps.DIVIDE:                 CurrentVal /= ValIn;                 break;             default:                 if (helper != null)                     CurrentVal = helper.Calc(CurrentVal, op, ValIn);                 else                     throw new java.lang.Exception("INVALID COMMAND.");         }         return CurrentVal;     }     public synchronized void Clear() {         CurrentVal = 0;         return;     }     protected void finalize() {         helper = null;     } } 

You can create an instance of a DumbCalc object in one of three ways, using the three overloaded constructors. You can overload other class methods in the same way. The only requirement is that each form must have a distinct signature (different parameter types or number of parameters).

The synchronized keyword was introduced in the DumbCalc example; this keyword is used to provide thread safety. Only one synchronized method can be executing at any given time. In the context of Listing 52.1, this prevents one thread from clearing the result while another thread is performing an operation through CalcResult. Separate threads typically would use separate instances, but it is safer to assume the worst.

Listing 52.1 demonstrates the power of Java's capability to separate the interface from the implementation. You can use any object implementing the CalcHelper interface to extend the capabilities of DumbCalc. You cannot extend the class by using inheritance because it is declared final.

Page 1155

Import statements are used to include definitions in other packages. The java.lang package is implicitly included, but it doesn't hurt to explicitly include its contents. Classes and interfaces with the same name can exist in separate packages, so the caller should use the package name to fully qualify a reference when it first is declared or created, as in this example:

 Calc.DumbCalc calc = new Calc.DumbCalc(); 

Each Java applet or stand-alone program must have an entry point. A browser-based applet must extend java.applet.Applet and implement the init() method. Stand-alone Java applications must define a class containing main(), which will be used to start the application. The main() method must be declared as the following:

 public static void main(String[] args) {} 

The array of strings holds the command-line arguments passed to the interpreter.

TIP
The concepts and examples presented in this section should give you a basic understanding of Java. More information is available online from Sun and JavaSoft (the developers of Java) at www.javasoft.com or java.sun.com

Advantages of Using Java

The write once, run everywhere strategy is the primary advantage of using Java. Although most third-generation languages are compiled into platform-specific native code, Java is compiled into byte code that is interpreted by a virtual machine. This means that Java classes will run on any platform that has a virtual machine, regardless of the platform on which the classes were compiled.

Java offers the additional advantages of true object-orientation, automatic garbage collection, and a robust set of libraries, which make it very easy to use. Java's multithreaded capabilities and network libraries make it suitable for developing server components , as well as browser-based applets and stand-alone clients .

Other design features make Java applets and applications easier to distribute, maintain, and create distributed systems. The applet model and the Remote Method Invocation (RMI) bootstrap loader enable all code to be housed centrally on the server; clients automatically download the latest version each time the code is accessed. The Java object model can simplify development

Page 1156

and application maintenance because you can use it to separate an interface from an implementation. It is this feature, in particular, that aligns Java with the goals of CORBA, which is described in the following section.

CORBA Fundamentals

CORBA is a distributed computing standard for object-oriented technologies developed by the OMG. This section provides a brief overview of the standard, which defines a language- neutral object model, an architecture for distributed object computing, specifications for interoperability, a language-neutral interface definition language (IDL), and several language-specific IDL mappings.

The CORBA Object Model

The CORBA object model is unique because it is based on a platform and language-neutral, distributed computing view of objects. The specification defines a full set of object semantics, but the definitions can be somewhat confusing outside the context of IDL, which is described in "Interface Definition Language (IDL)," later in this chapter. For now, the important points of CORBA follow:

  • An object is a unique, identifiable instance of a constructed type that provides services that can be requested of a client. An object reference is a value that can be used to identify a valid instance of a constructed type (typically used by clients to refer to a server-based object).
  • The CORBA definition of type is broad, but it distinguishes basic types (integers, characters , and so on) from constructed types (structs, unions, and so on).
  • An interface is a set of services that can be requested and can be satisfied by one or more objects.
  • An interface can have attributes. Declaring an attribute in an interface is equivalent to defining get() and set() methods to retrieve and assign the value of the attribute.
  • Servers called object implementations provide the execution of requested services, store or retrieve data, and maintain state.

The CORBA specification also defines parameters, exceptions, and additional terms, but these are the key features of the CORBA object model.

NOTE
There is no concept of a class in the CORBA object model. However, a specific programming language can define a class as an object implementation that satisfies one or more interfaces.
Previous Table of Contents Next


Oracle Unleashed
Oracle Development Unleashed (3rd Edition)
ISBN: 0672315750
EAN: 2147483647
Year: 1997
Pages: 391

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