This forum has been archived. Please start a new discussion on GitHub.

Ice struct generated constructor discrepancy between Python and Java generated code.


I am using Ice 3.5.0 on Linux, Windows and Mac OSX. C++ on the server with Python, Java and C++ clients.

For a simple slice file example as below:

module example {

struct ItemKey {

    string name; 



#include <>

module example {

struct ItemRec {

    int  priority = 7;
    bool active;

    ItemKey key;

    string  description = "foo";


I see differences in the generated constructor for ItemRec between the Java and python code.

From for python:
     class ItemRec(object):
        def __init__(self, priority=7, active=False, key=Ice._struct_marker, description="foo"):
            self.priority = priority
   = active
            if key is Ice._struct_marker:
                self.key = _M_example.ItemKey()
                self.key = key
            self.description = description

Here, even if the 'key' field is not populated, the python code automatically constructs a default ItemKey and uses it.

From for java, the default constructor only initializes fields that have a default value in the ice file:
    public ItemRec()
        priority = 7;
        description = "foo";

This ends up leaving the 'key' as null, if it is not explicitly initialized.

The __write method for ItemRec is implemented as:
    public void
    __write(IceInternal.BasicStream __os)

When __write(..) gets invoked, a NullPointerException gets thrown if the key is null. This problem does not happen when using python on the client side as a default ItemKey is constructed for 'key'.

Why is there this difference? Is there any slice option (or fix) to make the generated Java default constructor initialize all the fields of a struct/class similar to the generated python code?

What I am expecting would be something like:
    public ItemRec()
        priority = 7;
        active = false;
        key = new ItemKey();
        description = "foo";



  • benoit
    benoit Rennes, France
    Hi Shankar,

    Yes, Java/C# mappings are different from Python/Ruby mappings here.

    In Python and Ruby, generated structs have a single constructor which provides default values for struct/enum/string members.

    In Java and C#, generated structs have 2 constructors. The default constructor (no parameters) initializes members to their default value (which is "null" for reference types). The "one-shot" constructor allows you to pass the values to assign to the members.

    So, you need to assign the struct data members in Java even when using the default constructor.

    Note that for Slice class members, Python and Java behave the same way: the class member is set to "None" in Python and to "null" in Java by default.