Fields
Syntax
A field is defined as Type name, where Type is the field's type and name is the field's name. For example:
string name;
ByteSeq image;
Fruit fruit;
Each field definition must be followed by a semicolon. You can define several fields on the same line if you wish, as in:
struct Point { int x; int y; }
The type of a field can be a basic type, a user-defined type, Object* (an untyped proxy), or Value(an untyped class instance).
Nullable and Non-Nullable Fields
Fields with proxy and class types are nullable. Optional fields (presented below) are also nullable. All other fields are non-nullable, meaning they must have a value when transmitted through Ice.
struct Example
{
int count; // must be set
Greeter* greeter; // proxy type, so it can be null
Node node; // class type, so it can be null
}
Optional Fields
A field of a Slice class or exception may be declared as optional to indicate that a program can leave its value unset. Fields not declared as optional are known as required fields; a program must supply legal values for all required fields.
Each optional field must be assigned a unique, non-negative integer tag:
module M
{
class C
{
string name;
bool active;
optional(2) string alternateName;
optional(5) int overrideCode;
}
}
It is legal for a base type's tag to be reused by a derived type:
exception BaseException
{
optional(1) int systemCode;
}
exception DerivedException extends BaseException
{
optional(1) string diagnostic; // OK
}
The scope of a tag is limited to its enclosing type and has no effect on base or derived types.
You can mark any field as optional, except if the field’s type is a class type or a constructed type that holds a class type. At the field level, optional and class are incompatible.
Optional fields and required fields can appear in any order in your class definition. You can also assign tags in any order. For example:
class UnusualButValidPerson
{
optional(5) string nickname;
string name;
optional(0) Date dateOfBirth; // Date is a struct
string currentAddress;
optional(123) Fruit favoriteFruit; // Fruit is an enum
}
Default Values
You can specify a default value for a field that has one of the following types:
An integral type (
byte,short,int,long)A floating point type (
floatordouble)
For example:
struct Location
{
string name;
Point pt;
bool display = true;
string source = "GPS";
}
The legal syntax for literal values is the same as for Slice constants, and you may also use a constant as a default value. The language mapping guarantees that fields are initialized to their declared default values using a language-specific mechanism.
You can declare a default value for optional fields just as you can for required fields:
class C
{
string name;
bool active = true;
optional(2) string alternateName;
optional(5) int overrideCode = -1;
}
An optional field with a default value is considered to be set by default.
Language Mapping
A Slice field maps to a Java field with the same name. The type of the Java field is the mapped Slice type. This is the default mapping.
For example:
class Address { ... }
struct Person
{
string name;
Address address;
}
maps to:
public final class Employee implements java.lang.Cloneable, java.io.Serializable {
public String name; // Slice string maps to Java String
public Address address; // Slice Address maps the Java Address
...
}
JavaBean Mapping
Use the metadata directive java:getset to map a Slice field to two or more JavaBean-style methods instead of a public field.
For each field val of type T, the mapping generates the following methods:
public T getVal();
public void setVal(T v);
The mapping generates an additional method if T is the bool type:
public boolean isVal();
Finally, if T is a sequence type with an element type E, two methods are generated to provide direct access to elements:
public E getVal(int index);
public void setVal(int index, E v);
Note that these element methods are only generated for sequence types that use the default mapping.
You can apply the java:getset directive to an individual field, or to enclosing construct, as illustrated by the following example:
sequence<int> IntSeq;
class C
{
["java:getset"] int i;
double d;
}
["java:getset"]
struct S
{
bool b;
string str;
}
["java:getset"]
exception E
{
IntSeq seq;
}
JavaBean get-set methods are generated for all fields of struct S and exception E, but for only one field of class C. Relevant portions of the generated code are shown below:
public class C extends com.zeroc.Ice.Value {
private int i;
public double d;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
public final class S implements java.lang.Cloneable, java.io.Serializable {
private boolean b;
private java.lang.String str;
public boolean getB() {
return b;
}
public void setB(boolean b) {
this.b = b;
}
public boolean isB() {
return b;
}
public java.lang.String getStr() {
return str;
}
public void setStr(java.lang.String str) {
this.str = str;
}
...
}
public class E extends com.zeroc.Ice.UserException {
private int[] seq;
public int[] getSeq() {
return seq;
}
public void setSeq(int[] seq) {
this.seq = seq;
}
public int getSeq(int index) {
return this.seq[index];
}
public void setSeq(int index, int val) {
this.seq[index] = val;
}
...
}
Optional Fields
The mapping for optional fields in Slice classes and exceptions uses a JavaBean-style API that provides methods to get, set, and clear a field’s value, and test whether a value is set. Consider the following Slice definition:
class C
{
string name;
optional(2) string alternateName;
optional(5) bool active;
}
The generated Java code provides the following API:
public class C extends com.zeroc.Ice.Value {
public String getAlternateName()...
public void setAlternateName(String alternateName)...
public boolean hasAlternateName()...
public void clearAlternateName()...
public void optionalAlternateName(java.util.Optional<String> v)...
public java.util.Optional<String> optionalAlternateName()...
public boolean isActive()...
public boolean getActive()...
public void setActive(boolean v)...
public boolean hasActive()...
public void clearActive()...
public void optionalActive(java.util.Optional<Boolean> v)...
public java.util.Optional<Boolean> optionalActive()...
...
}
The has method allows you to test whether a field’s value has been set, and the clear method removes any existing value for a field.
Calling a get method when the field’s value has not been set throws java.util.NoSuchElementException.
The optional methods provide an alternate API that uses standard Java types to encapsulate the value:
java.util.OptionalDouble
Encapsulates a value of typedoublejava.util.OptionalInt
Encapsulates a value of typeintjava.util.OptionalLong
Encapsulates a value of typelongjava.util.Optional<T>
Encapsulates all other Slice types
Default Values
Slice default values change the implementation of the parameterless constructor of the enclosing type.
For example:
struct Location
{
string name;
Point point;
bool display = true;
string source = "GPS";
}
maps to:
public final class Location implements java.lang.Cloneable, java.io.Serializable {
public String name;
public Point point;
public boolean display;
public String source;
public Location() {
this.name = "";
this.point = new Point();
this.display = true;
this.source = "GPS";
}
...
}
When you don’t define a default value in Slice, and you initialize a field without providing a value for this field, the generated code uses the following default:
Optional Field? | Slice Field Type | Default Java Value |
|---|---|---|
No |
| Empty string |
| First enumerator in enumeration | |
| New instance created with no argument | |
Numeric |
| |
|
| |
|
| |
Yes | Any | Not set |