Objects

Note that while there are eight primitive types, there are many thousands of object types (also know as classes) in the Java runtime environment and over one thousand classes in the Patchworks system. Classes are specialized to model certain data types or handle specific tasks. Classes are programmed with the names and data types of the local values that the object will contain, and the methods (you may know them as functions or subroutines) that can operate on the local data.

[Tip]Java API

Search the web for the “Java API” for more information about the Java class system. Have a look at the Patchwork API to see what classes are available. By convention class names always begin with a capital letter, as opposed to primitive value type names that always start with a lower case letter.

Classes are the template patterns for objects, and objects are the instances that are created from the templates. In the jargon of Object Oriented Programming (OOP) objects are also described as instances of a class, and these two terms mean the same thing.

The ‘new’ operator can be used to create an object of the same type as the class name. In this example we instantiate (or create) a String object:

firstName = new String{"John");

We usually say that the firstName variable contains the object, but more precisely it contains a reference to the object which is actually stored in heap memory. Once we have a reference to the String object there are a large number of methods that we can call to perform various actions, for example

firstName = new String("John");
print("Hello "+firstName.toUpperCase());

When we call the toUpperCase method the original String object returns a new String object that has the same letters but all in upper case. The original String object referred to by the firstName variable remains unchanged.

[Tip]String class

See the Java API documentation for the String class. Read the description of the class and review the methods that this class provides.

[Note]Note

The following two statements have an identical effect:

firstName = new String("John");
firstName = "John";

The second form saves typing a few characters and looks more conventional as compared to other languages. This is an example of is what language designers call ‘syntactic sugar’, which is adding features to the language that do the same thing as an existing technique but are sweeter to use. In this sense BeanShell itself is syntactic sugar for Java: it can do many of the same things, but often with many fewer keystrokes.

Besides having methods, some objects will have publicly available fields that may be read or written. When available the syntax to access these field is:

a = obj1.field1;     // read field1 from the object
Obj1.field1 = 123;   // set field1 in the object.

Most public APIs do not support classes having public fields, because this breaks encapsulation and is often a bad programming practice. As we will see later, scripted objects can gainfully use accessible fields to simply automation code.

The Java object system also support inheritance, which is the concept that a class can be defined to inherit all of the fields and methods of a parent class, and then extend and specialize it's own behaviour. For example, in the Patchworks API there is a base Report class that defines common methods for defining and writing reports. The various report templates (StandardReport, PeriodCategoryReport, etc) all inherit the basic report capabilities, but then add their own flavour of summarizing and presenting. When the API notes that a Report data type is required, then any of the report types can be provided because they are all sub-types of Report. Search the web for "Java inheritance" for more information, and also search for "Java interfaces" for information about the related concept of interface inheritance.

As mentioned, object variables contain references to the objects stored on the heap. Object variables can contain a special value ‘null’ that indicates that no value has been assigned. Null is the value that is assigned when an object cannot be found, such as trying to retrieve an object that does not exist in a dictionary or reading a line from a file that has no more lines. Null is also the initial value for object variable that have been declared but not assigned to. It is an error to call a method or to try to retrieve a field value from a null object: in this situation a NullPointerException will be thrown.