# Class KeyFactory.Builder (5.0.3)

    public static final class KeyFactory.Builder

Helper class that aids in the construction of [Keys](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) with ancestors. Initialize the
`Builder` with the topmost ancestor in your key path and then add children using the
[#addChild](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory.Builder#com_google_appengine_api_datastore_KeyFactory_Builder_addChild_) overload that best suits your needs. When finished adding children, call
[#getKey()](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory.Builder#com_google_appengine_api_datastore_KeyFactory_Builder_getKey__) to retrieve your [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) or [#getString()](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory.Builder#com_google_appengine_api_datastore_KeyFactory_Builder_getString__) to retrieve your [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) encoded as a websafe [String](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html).

Examples:  


     import com.google.appengine.api.datastore.KeyFactory.Builder;

     ...

     Key key = new Builder("Person", 88).addChild("Address", 24).getKey();
     String keyStr = new Builder("Photo Album", "Vacation").addChild("Photo", 1424).getString();
     
## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> KeyFactory.Builder

## 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-)

## Constructors

### Builder(Key key)

    public Builder(Key key)

Create a `Builder`, establishing the provided [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) as the topmost ancestor.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `key` | `https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key` the topmost ancestor |

### Builder(String kind, String name)

    public Builder(String kind, String name)

Create a `Builder`, establishing a [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) constructed from the provided kind and
name as the topmost ancestor.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `kind` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the kind of the topmost ancestor |
| `name` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the name of the topmost ancestor in `kind`, as an arbitrary string unique across root entities of this `kind` |

### Builder(String kind, long id)

    public Builder(String kind, long id)

Create a `Builder`, establishing a [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) constructed from the provided kind and
id as the topmost ancestor.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `kind` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the kind of the topmost ancestor |
| `id` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` the numeric identifier of the topmost ancestor in `kind`, unique across root entities of this kind, must not be zero |

## Methods

### addChild(String kind, String name)

    public KeyFactory.Builder addChild(String kind, String name)

Add a [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) constructed from the provided kind and name as the child of the [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key)
most recently added to the `Builder`.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `kind` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the kind of the child |
| `name` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the name of the child in `kind`, as an arbitrary string unique across entities of this `kind` with the same parent |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory.Builder` | `this` |

### addChild(String kind, long id)

    public KeyFactory.Builder addChild(String kind, long id)

Add a [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) constructed from the provided kind and id as the child of the [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key)
most recently added to the `Builder`.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `kind` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the kind of the child |
| `id` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` the numeric identifier of the child in `kind`, unique across entities of this kind with the same parent, must not be zero |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory.Builder` | `this` |

### getKey()

    public Key getKey()

Returns the most recently added [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key` |   |

### getString()

    public String getString()

Returns the most recently added [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key), encoded as a websafe [String](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |