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!:

LET X! = X
LET Y! = 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:

LET A! = new java.util.ArrayList()
LET X! = A!

Some objects define a clone() method to create a copy of a complex data structure. See the Java documentation for details. For example:

LET A! = new java.util.ArrayList()
LET X! = A!.clone()

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:

HelloWorld! = new HelloWorld()
HelloWorld!.hello()

class public HelloWorld
  method public void hello()
    print "Hello, World!"
  methodend
classend

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:

server_date! = new java.util.Date()
client_date! = new java.util.Date@()

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:

sysgui = unt
open (sysgui)"X0"
sysgui! = bbjapi().getSysGui()
window! = sysgui!.addWindow(100,100,200,150,"JComponent",$00090083$)
window!.setCallback(window!.ON_CLOSE,"eoj")
editbox! = window!.addEditBox(101,25,25,150,25,"BBjEditBox",$$)
component! = cast(javax.swing.JComponent@,editbox!)
border! = new javax.swing.border.LineBorder@(java.awt.Color@.red,2,1)
component!.setBorder(border!)
JButton! = new javax.swing.JButton@()
BBjControl! = window!.addWrappedJComponent(102,25,75,150,25,JButton!)
BBjControl!.setText("JButton")
process_events
eoj:
release

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:

0010 loop: INPUT "Enter an integer value: ",integer$
0020 integer = new Integer(integer$,ERR=loop)
0030 PRINT integer$," =",integer

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:

use java.awt.Color
declare Color[] Color!
color! = new Color[2]
color![0] = Color.RED
color![1] = Color.BLUE
print "color![0]=",color![0]
print "color![1]=",color![1]

See Also

BBj Object Variables

BBj Object Error Handling

BBj Object Operators

Working with Java Arrays

CAST() Function