NetRexx at Once

Quick Guide for Java Developers


[Index] [Previous Chapter] [Next Chapter]

2. Classes and Objects

What we have seen previously is useful for small applications. What about more complex applications and applets? We need to declare classes, fields, methods and to create objects. For example, consider this case: we want to write a simple Applet that read a String parameter (given with the PARAM tag) and write it in white on a red background. Let's see what we need.

2.1 CLASS Instruction

We want to create a class file to be used as an Applet by a Browser. We need to extend the Java class java.applet.Applet and we start as follows:

Java
/* This is MyApplet.java */
import java.applet.Applet;
public class MyApplet extends Applet {
//... list of fields and methods
}
NetRexx
/* This is MyApplet.nrx */
class MyApplet extends Applet
--... list of properties and methods
--
--
notes
  • No import needed for standard Java packages.
  • Fields (variables in classes) are called properties.
CLASS Instruction Syntax

class class_name [private|public|shared] [abstract|final|interface|adapter] [binary]
[extends parent_class_name]
[uses used_class_name[,...]]
[implements interface_name[,...]];

visibility

    public visible to all classes (default for the first class in a file)
    private
    visible within the current package (default for all other classes in a file)
    shared
    visible within the current package (same as private)

modifiers

    final cannot be extended
    abstract
    some method is abstract
    interface
    all methods are abstract and properties are constants
    adapter
    implements (automatically) all inherited abstract methods

binary forces the use of native types

extends indentifies the superclass

uses lists classes sources of static properties and methods

implements lists interfaces inherited

2.2 PROPERTIES Instruction

Let's add a field to the Applet. In the init method we'll read the parameter MSG and then its value will be stored in a variable.

Java
/* This is MyApplet.java */
import java.applet.Applet;
public class MyApplet extends Applet {
String message;
//... list of methods
}
NetRexx
/* This is MyApplet.nrx */
class MyApplet extends Applet
message=String -- inheritable by default
--... list of methods
--
--
notes
  • Fields don't need a type: it's set according to the assignment.
  • The initial value is null (different from message=String()).
  • The properties instruction is used to change visibility (inheritable by default).
PROPERTIES Instruction Syntax

properties [public|inheritable|private|shared] [constant|static|volatile|transient|indirect];

visibility

    public visible to all classes in which the current class is visible
    inheritable
    visible within the current package and to classes that extends the current class (default)
    shared
    visible within the current package but not inherited by classes that extends the current class
    private
    visible within the current class

modifiers

    constant unchangeable property of the class
    static
    changeable property of the class
    volatile
    may change asynchronously
    transient
    not saved when the object is serialized
    indirect
    accessed through (automatically generated) get-set methods (JavaBeans convenction)

2.3 METHOD Instruction

Finally, we need the init() method (to initialize the message field reading the MSG parameter) and the paint(Graphics g) method (to show the message).

Java
/* This is MyApplet.java */
import java.applet.Applet;
public class MyApplet extends Applet {
String message;
public void init() {
setBackground(Color.red);
setForeground(Color.white);
message = getParameter("MSG");
}
public void paint(Graphics g) {
g.drawString(message,10,10);
}
}
NetRexx
/* This is MyApplet.nrx */
class MyApplet extends Applet
message=String
method init
setBackground(Color.red)
setForeground(Color.white)
message=getParameter("MSG")
method paint(g=Graphics)
g.drawString(message,10,10)
--
--
--
--
  • Methods without arguments don't need parentheses.
  • Arguments are declared with an assignment.
  • Default values may be associated to arguments through the assignment mechanism.
  • No brakets needed to end the block of a method.
METHOD Instruction Syntax

method method_name[([argument_name[,...]])]
[public|inheritable|private|shared]
[constant|static|abstract|final|native] [protect]
[returns return_type]
[signal exception_name[,...]];

visibility

    public visible to all classes in which the current class is visible
    inheritable
    visible within the current package and to classes that extends the current class (default)
    shared
    visible within the current package but not inherited by classes that extends the current class
    private
    visible within the current class

modifiers

    static method of the class
    final
    cannot be overridden
    constant
    static and final
    abstract
    not implemented
    native
    implemented externally

protect protects the current object (synchronization)

returns defines the type returned (default NetRexx String)

signal lists throwable exceptions

2.3.1 Default Values for Arguments

Default values for the arguments of a method are an useful feature available in C++. Java doesn't possess it, but NetRexx does. Imagine a class that often needs to pass the same argument to one of its methods. With Java you can add another method with the same name that uses a default value and recalls the original one, otherwise you are obliged to pass that value every time you need it. In NetRexx it's sufficient to declare a value for that argument in the argument list. For example, consider a Sphere class that must be scaled (often by a 2 factor):

Java
/* This is Sphere.java */
public class Sphere {
private float radius = 1.0;
Sphere(float radius) {
this.radius = radius;
}
public void Scale(float factor) {
radius *= factor;
}
public void Scale() {
Scale(2.0);
}
}

// In another class
Sphere sph = new Sphere();
sph.Scale(); // doubles the radius
sph.Scale(0.5); // halves the radius
NetRexx
/* This is Sphere.nrx */
class Sphere
properties private
radius
method Sphere(initradius=1.0)
radius=initradius
method Scale(factor=2.0)
radius = radius * factor
--
--
--
--
--

-- In another class
sph=Sphere
sph.Scale -- doubles the radius
sph.Scale(0.5) -- halves the radius
  • Variables may have no type (REXX by defaut).
  • Default arguments are declared as an assignment.
  • No op= operators.
  • PROPERTIES is used to change default visibility of properties.
  • Method arguments cannot match property names

2.3.2 Returning Values

A method returns values with the return keyword. For example, let's change the Sphere class in a more configurable one:

Java
/* This is Sphere.java */
public class Sphere {
private float radius = 1.0;
public Sphere(float r) {
radius = r;
}
public float getRadius() {
return radius;
}
public void setRadius(float r) {
radius = r;
}
}
NetRexx
/* This is Sphere.nrx */
class Sphere
properties private
radius
method Sphere(r=1.0)
radius=r
method getRadius
return radius
method setRadius(r)
radius=r
--
--
--
NetRexx (experimental)
/* This is Sphere.nrx */
class Sphere
properties indirect
radius
method Sphere(r=1.0)
radius=r
--
--
--
--
--
--
--
  • Variables may have no type (REXX by defaut).
  • Methods may have no return type (REXX by defaut).
  • Indirect visibility is used to generate automatically methods
    for an easy access of properties (JavaBeans convenctions).

2.4 Constructors

Objects are created using special methods called constructors. Constructors, as in Java, have the same name of the class. Instances of classes (objects) are created without the new keyword and, as in normal methods, arguments may have default values.

Java
/* Examples of new Java objects */
Vector v1 = new Vector(); // empty Vector
Vector v2 = new Vector(10); // with capacity
NetRexx
/* Examples of new NetRexx objects */
v1=Vector() -- an empty Vector
v2=Vector(10) -- with capacity
  • new keyword isn't needed.
  • Constructors need parentheses even if no arguments are supplied.
  • Available default values for arguments.
  • No type declaration (implied in assignment).

[Index] [Previous Chapter] [Next Chapter]