# Class SingleFieldBuilder&lt;MType,BType,IType&gt; (3.19.4)

    public class SingleFieldBuilder<MType,BType,IType> implements GeneratedMessage.BuilderParent

`SingleFieldBuilder` implements a structure that a protocol message uses to hold a single
field of another protocol message. It supports the classical use case of setting an immutable
[Message](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Message) as the value of the field and is highly optimized around this.

It also supports the additional use case of setting a [Message.Builder](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Message.Builder) as the field and
deferring conversion of that `Builder` to an immutable `Message`. In this way, it's
possible to maintain a tree of `Builder`'s that acts as a fully read/write data structure.


Logically, one can think of a tree of builders as converting the entire tree to messages when
build is called on the root or when any method is called that desires a Message instead of a
Builder. In terms of the implementation, the `SingleFieldBuilder` and `
RepeatedFieldBuilder` classes cache messages that were created so that messages only need to be
created when some change occurred in its builder or a builder for one of its descendants.

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> SingleFieldBuilder\<MType,BType,IType\>

## Implements

[GeneratedMessage.BuilderParent](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.GeneratedMessage.BuilderParent)

## Inherited Members

[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

| ## Type Parameters ||
|---|---|
| **Name** | **Description** |
| `MType` |   |
| `BType` |   |
| `IType` |   |

## Constructors

### SingleFieldBuilder(MType message, GeneratedMessage.BuilderParent parent, boolean isClean)

    public SingleFieldBuilder(MType message, GeneratedMessage.BuilderParent parent, boolean isClean)

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `message` | `MType` |
| `parent` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.GeneratedMessage.BuilderParent` |
| `isClean` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

## Methods

### build()

    public MType build()

Builds the message and returns it.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `MType` | the message |

### clear()

    public SingleFieldBuilder<MType,BType,IType> clear()

Clears the value of the field.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.SingleFieldBuilder<MType,BType,IType>` | the builder |

### dispose()

    public void dispose()

### getBuilder()

    public BType getBuilder()

Gets a builder for the field. If no builder has been created yet, a builder is created on
demand by calling [Message#toBuilder](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Message#com_google_protobuf_Message_toBuilder_).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `BType` | The builder for the field |

### getMessage()

    public MType getMessage()

Get the message for the field. If the message is currently stored as a `Builder`, it is
converted to a `Message` by calling [Message.Builder#buildPartial](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Message.Builder#com_google_protobuf_Message_Builder_buildPartial_) on it. If no
message has been set, returns the default instance of the message.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `MType` | the message for the field |

### getMessageOrBuilder()

    public IType getMessageOrBuilder()

Gets the base class interface for the field. This may either be a builder or a message. It will
return whatever is more efficient.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `IType` | the message or builder for the field as the base class interface |

### markDirty()

    public void markDirty()

A builder becomes dirty whenever a field is modified -- including fields in nested builders
-- and becomes clean when build() is called. Thus, when a builder becomes dirty, all its
parents become dirty as well, and when it becomes clean, all its children become clean. The
dirtiness state is used to invalidate certain cached values.

To this end, a builder calls markDirty() on its parent whenever it transitions from clean
to dirty. The parent must propagate this call to its own parent, unless it was already dirty,
in which case the grandparent must necessarily already be dirty as well. The parent can only
transition back to "clean" after calling build() on all children.

### mergeFrom(MType value)

    public SingleFieldBuilder<MType,BType,IType> mergeFrom(MType value)

Merges the field from another field.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `value` | `MType` the value to merge from |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.SingleFieldBuilder<MType,BType,IType>` | the builder |

### setMessage(MType message)

    public SingleFieldBuilder<MType,BType,IType> setMessage(MType message)

Sets a message for the field replacing any existing value.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `message` | `MType` the message to set |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.SingleFieldBuilder<MType,BType,IType>` | the builder |