BBj Object Creation and Assignment
Description
Assignment of a number or string to an object variable copies the value. For example, after executing these lines of code, subsequent changes to X will not affect X!and subsequent changes to Y$ will not affect Y!:
|
Assignment of any other value to an object variable copies a reference. For example, after executing these lines of code, subsequent changes to A! will be reflected in X!, because they are pointers to the same ArrayList:
|
Some objects define a clone() method to create a copy of a complex data structure. See the Java documentation for details. For example:
|
The new operator creates a new Java object or BBj custom object. Object-oriented language structures are case-sensitive, and unlike BBx verbs and functions, the new operator must be specified in lower-case:
>a! = new java.util.ArrayList()
>a! = New java.util.ArrayList()
!ERROR=20 (Syntax Error: a! = New java.util.ArrayList())
>
The new operator can be used to create a new BBj custom object. The BBj class that defines the structure of the object can be contained in the same program, or in an external program referenced with a USE verb:
|
The new operator can also be used to create a new Java object:
>x! = new java.util.ArrayList()
>x!.add("hello")
>x!.add("world")
>print x!
[hello, world]
>
BBj is a client/server environment, and all of the examples so far have created objects on the server, where the BBj program lives. Sometimes you want to create an object on the client. For these cases, BBj defines a special notation for the class reference, a "@" suffix. For example:
|
The most common use case for client objects is to directly manipulate the client-side Java Swing JComponent that corresponds to a BBjControl, or to embed an arbitrary Java Swing JComponent on a BBjWindow:
|
Because client object component references depend on the assumption that the client is Java Swing, they only work with the Java Swing GUI client, not the BUI and DWC browser clients.
BBj object-oriented references support BBx standard error handling functionality, including the SETERR verb and the ERR= option. This is typically used when calling object-oriented functions with unknown data, but it's also available with object creation:
|
A Java expression that returns a Java array can be assigned directly to a BBj array:
REM ' Locale![] is a BBj array of java.util.Locale objects
LET Locale![] = java.text.NumberFormat.getAvailableLocales()
REM ' IP![] is a BBj array of bytes representing the local IP address
LET IP![] = java.net.InetAddress.getLocalHost().getAddress()
DUMP (0,MODE="NAME=IP![]")
>run
****** LEVEL 0, PGM=scratch.txt
IP![] has 4 elements [3]
[0] = 127 (byte)
[1] = 0 (byte)
[2] = 0 (byte)
[3] = 1 (byte)
READY
>
In BBj 18.00 and higher, Java arrays can be created with the new operator, and can be directly manipulated using syntax similar to that of traditional BBj arrays:
|