---
title: "Structures"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures
language: java
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Structures

Programming language: Java. Other languages: [C++](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/csharp.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/swift.md).

## Struct Syntax

A structure groups one or more named fields, of any type, into a new type. For example:

```slice
module M
{
    struct TimeOfDay
    {
        short hour;         // 0 - 23
        short minute;       // 0 - 59
        short second;       // 0 - 59
    }
}
```

This definition introduces a new type named `TimeOfDay`. The name of a field must be unique within its structure.

A structure contains only fields: as elsewhere in Slice, type definitions cannot be nested, except for [modules](https://docs.zeroc.com/ice/3.8/slice/modules/java.md). Like every other type, you define a structure in a module.

## Language Mapping

A Slice structure maps to a Java class with the same name. For each Slice field, the Java class contains a corresponding public field. For example, here is our Employee structure once more:

```slice
struct Employee
{
    long number;
    string firstName;
    string lastName;
}
```

The Slice-to-Java compiler generates the following definition for this structure:

```java
public final class Employee implements java.lang.Cloneable, java.io.Serializable {
    public long number;
    public String firstName;
    public String lastName;

    public Employee() {
        this.firstName = "";
        this.lastName = "";
    }

    public Employee(long number, String firstName, String lastName) {
        this.number = number;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public boolean equals(java.lang.Object rhs) ...

    @Override
    public int hashCode() ...

    @Override
    public Employee clone() ...
}
```

You can optionally customize the mapping for [fields](https://docs.zeroc.com/ice/3.8/slice/fields/java.md) to use getters and setters instead.

The `equals` method compares two structures for equality. Note that the generated class also provides the usual `hashCode` and `clone` methods. (`clone` has the default behavior of making a shallow copy.)

### Generated Constructors

The mapped Java class provides two constructors:

- a constructor with a parameter for each field
- a parameterless constructor that initializes all fields to default values (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/java.md))

## See Also

- [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/java.md)
- [Classes](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/java.md)
