Java Reference
In-Depth Information
But wait, the sum of 4 and 9 should be 13, not 32! What went wrong? Actually,
nothing went wrong, because we coded the
add()
method to add the two input
parameters
plus
the value of
COR19
-
EXAMPLE
-
CONSTANT
from the IDL file.
We didn't show that code above, but it appears here and on the Web Course:
public int add (int a, int b)
{
return a + b + Cor19Example.COR19
-
EXAMPLE
-
CONSTANT;
}
There are other methods defined in the
server.idl
file that we have not tested,
such as the getter and setter methods for the
huh
and
hah
attributes. Test code
in the client for those methods is:
// Try getting and setting the huh and hah attributes.
System.err.println ("hah() ="+ cor19.hah ());
System.err.println ("Before setting, huh() ="+
cor19.huh ());
cor19.huh (19);
System.err.println (
"
After setting, huh()
="
+
cor19.huh ());
To demonstrate the use of the Holder object for the
inout CustomData
param-
eter, we first create and populate a
CustomData
object.
CustomData.java
is one of the files created by the IDL compiler - its function is to serve as the
CustomData struct
defined in the IDL file.
There are two ways to populate the
CustomData
object. One is to supply the
values as parameters to the
CustomData()
constructor since the
CustomData
class generated by
idlj
includes an overloaded constructor that receives values
for each field in the
struct
.For example,
// Create and populate a CustomData object.
CustomData cd
=
new CustomData (12.0f, -13);
This technique requires only one line of code and is especially useful when the
values of all the fields are known at construction time.
If some values are not known until later, then an alternative method is required.
There is also a no-arg constructor that merely creates an empty
CustomData
object. The
idlj
-generated
CustomData
class implements all the structure
fields as public variables, which means we can directly access the fields. (Imple-
menting fields as public variables is generally frowned upon as poor OO design,
but that's the way CORBA does it.) Each field name in
CustomData
is named
with the same name used in the structure definition in the IDL file. Therefore,
the alternative way to create and populate a
CustomData
object is:
// Create and populate a CustomData object.
CustomData cd
=
new CustomData ();
cd.someFloatValue
=
12.0f;
cd.someIntegerValue
=
-13;
Search WWH ::
Custom Search