# Class KeyFactory (5.0.3)

    public class KeyFactory

This class enables direct creation of `Key` objects, both in the root entity group (no
parent) and as the child of a given parent. Keys can also be created indirectly by putting named
Entities into the datastore, which will allocate a new key. See Entity#Entity(String, String) for details.

This class also has methods for encoding and decoding `Key` objects to and from strings.
Clients should not make any assumptions about the encoding format, except that it is a websafe
string that does not need to be quoted when used in HTML or in URLs.
See Also: [Entity](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Entity)

## Inheritance

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

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

## Static Methods

### createKey(@Nullable Key parent, String kind, String name)

    public static Key createKey(@Nullable Key parent, String kind, String name)

Creates a new `Key` with the provided parent from its kind and name.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `parent` | `com.google.appengine.api.datastore.@org.jspecify.annotations.Nullable Key` the parent of the key to create, can be `null` |
| `kind` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the kind of the key to create |
| `name` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the name of the key in `kind`, as an arbitrary string unique across all 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.Key` |   |

### createKey(@Nullable Key parent, String kind, long id)

    public static Key createKey(@Nullable Key parent, String kind, long id)

Creates a new `Key` with the provided parent from its kind and ID.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `parent` | `com.google.appengine.api.datastore.@org.jspecify.annotations.Nullable Key` the parent of the key to create, can be `null` |
| `kind` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the kind of the key to create |
| `id` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` the numeric identifier of the key in `kind`, unique across all 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.Key` |   |

### createKey(String kind, String name)

    public static Key createKey(String kind, String name)

Creates a new `Key` having no parent from its kind and name.

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

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

### createKey(String kind, long id)

    public static Key createKey(String kind, long id)

Creates a new `Key` having no parent from its kind and ID.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `kind` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the kind of the key to create |
| `id` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` the numeric identifier of the key in `kind`, unique across all root entities of this kind, 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.Key` |   |

### createKeyString(@Nullable Key parent, String kind, String name)

    public static String createKeyString(@Nullable Key parent, String kind, String name)

Shorthand for invoking [#keyToString(Key)](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory#com_google_appengine_api_datastore_KeyFactory_keyToString_com_google_appengine_api_datastore_Key_) on the result of #createKey(Key, String, String)

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `parent` | `com.google.appengine.api.datastore.@org.jspecify.annotations.Nullable Key` the parent of the key to create, can be `null`. |
| `kind` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the kind of the key to create |
| `name` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the name of the key in `kind`, as an arbitrary string unique across entities of this `kind` with the same parent |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` | A websafe `String` representation of the [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) that was created |

### createKeyString(@Nullable Key parent, String kind, long id)

    public static String createKeyString(@Nullable Key parent, String kind, long id)

Shorthand for invoking [#keyToString(Key)](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory#com_google_appengine_api_datastore_KeyFactory_keyToString_com_google_appengine_api_datastore_Key_) on the result of #createKey(Key, String, long)

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `parent` | `com.google.appengine.api.datastore.@org.jspecify.annotations.Nullable Key` the parent of the key to create, can be `null`. |
| `kind` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the kind of the key to create |
| `id` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` the numeric identifier of the key in `kind`, unique across entities of this kind with the same parent, must not be zero |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` | A websafe `String` representation of the [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) that was created |

### createKeyString(String kind, String name)

    public static String createKeyString(String kind, String name)

Shorthand for invoking [#keyToString(Key)](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory#com_google_appengine_api_datastore_KeyFactory_keyToString_com_google_appengine_api_datastore_Key_) on the result of #createKey(String, String)

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

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` | A websafe `String` representation of the [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) that was created |

### createKeyString(String kind, long id)

    public static String createKeyString(String kind, long id)

Shorthand for invoking [#keyToString(Key)](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory#com_google_appengine_api_datastore_KeyFactory_keyToString_com_google_appengine_api_datastore_Key_) on the result of #createKey(String, long)

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

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` | A websafe `String` representation of the [Key](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) that was created |

### keyToString(Key key)

    public static String keyToString(Key key)

Converts a `Key` into a websafe string. For example, this string can safely be used as an
URL parameter embedded in a HTML document. Note that `
key.equals(KeyFactory.stringToKey(KeyFactory.keyToString(key))
` should evaluate to `true`.

| **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 `Key` to convert to a `String`. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` | A websafe `String` representation of the provided `String`. |

### stringToKey(String encoded)

    public static Key stringToKey(String encoded)

Converts a `String`-representation of a `Key` into the `Key` instance it
represents. Note that `
str.equals(KeyFactory.keyToString(KeyFactory.stringToKey(str))
` should evaluate to `true` for all strings returned by [KeyFactory#keyToString](https://docs.cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory#com_google_appengine_api_datastore_KeyFactory_keyToString_).

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `encoded` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` The `String` representation of a `Key`. |

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