Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Protected Ctors](https://developer.android.com/reference/java/lang/ClassValue#proctors) \| [Methods](https://developer.android.com/reference/java/lang/ClassValue#pubmethods) \| [Protected Methods](https://developer.android.com/reference/java/lang/ClassValue#promethods) \| [Inherited Methods](https://developer.android.com/reference/java/lang/ClassValue#inhmethods)

# ClassValue

` public abstract class ClassValue `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.lang.ClassValue\<T\> |

<br />

*** ** * ** ***

Lazily associate a computed value with any `Class` object. For example, if a dynamic language needs to construct a message dispatch table for each class encountered at a message send call site, it can use a `ClassValue` to cache information needed to perform the message send quickly, for each class encountered.

The basic operation of a `ClassValue` is `https://developer.android.com/reference/java/lang/ClassValue#get(java.lang.Class<?>)`, which returns the associated value, initially created by an invocation to `https://developer.android.com/reference/java/lang/ClassValue#computeValue(java.lang.Class<?>)`; multiple invocations may happen under race, but exactly one value is associated to a `Class` and returned.

Another operation is `https://developer.android.com/reference/java/lang/ClassValue#remove(java.lang.Class<?>)`: it clears the associated value (if it exists), and ensures the next associated value is computed with input states up-to-date with the removal.

For a particular association, there is a total order for accesses to the associated value. Accesses are atomic; they include:

- A read-only access by `get`
- An attempt to associate the return value of a `computeValue` by `get`
- Clearing of an association by `remove`

A `get` call always include at least one access; a `remove` call always has exactly one access; a `computeValue` call always happens between two accesses. This establishes the order of `computeValue` calls with respect to `remove` calls and determines whether the results of a `computeValue` can be successfully associated by a `get`.

<br />

## Summary

| ### Protected constructors ||
|---|---|
| ` https://developer.android.com/reference/java/lang/ClassValue#ClassValue()() ` Sole constructor. |

| ### Public methods ||
|---|---|
| ` T` | ` https://developer.android.com/reference/java/lang/ClassValue#get(java.lang.Class<?>)(https://developer.android.com/reference/java/lang/Class<?> type) ` <br /> This method first performs a read-only access, and returns the associated value if it exists. |
| ` void` | ` https://developer.android.com/reference/java/lang/ClassValue#remove(java.lang.Class<?>)(https://developer.android.com/reference/java/lang/Class<?> type) ` Removes the associated value for the given `Class` and invalidates all out-of-date computations. |

| ### Protected methods ||
|---|---|
| ` abstract T` | ` https://developer.android.com/reference/java/lang/ClassValue#computeValue(java.lang.Class<?>)(https://developer.android.com/reference/java/lang/Class<?> type) ` Computes the value to associate to the given `Class`. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Protected constructors

### ClassValue

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected ClassValue ()
```

Sole constructor. (For invocation by subclass constructors, typically implicit.)

<br />

## Public methods

### get

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public T get (Class<?> type)
```

<br />

This method first performs a read-only access, and returns the associated value if it exists. Otherwise, this method tries to associate a value from a `https://developer.android.com/reference/java/lang/ClassValue#computeValue(java.lang.Class<?>)` invocation until the associated value exists, which could be associated by a competing thread.

This method may throw an exception from a `computeValue` invocation. In this case, no association happens.

<br />

| Parameters ||
|---|---|
| `type` | `Class`: the `Class` to retrieve the associated value for <br /> |

| Returns ||
|---|---|
| `T` | the value associated to the given `Class` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if the argument is `null` |

**See also:**

- `https://developer.android.com/reference/java/lang/ClassValue#remove(java.lang.Class<?>)`
- `https://developer.android.com/reference/java/lang/ClassValue#computeValue(java.lang.Class<?>)`

### remove

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void remove (Class<?> type)
```

Removes the associated value for the given `Class` and invalidates all out-of-date computations. If this association is subsequently [accessed](https://developer.android.com/reference/java/lang/ClassValue#get(java.lang.Class<?>)), this removal happens-before (JLS {@jls 17.4.5}) the finish of the `https://developer.android.com/reference/java/lang/ClassValue#computeValue(java.lang.Class<?>)` call that returned the associated value.

<br />

| Parameters ||
|---|---|
| `type` | `Class`: the type whose class value must be removed <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if the argument is `null` |

## Protected methods

### computeValue

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected abstract T computeValue (Class<?> type)
```

Computes the value to associate to the given `Class`.

This method is invoked when the initial read-only access by `https://developer.android.com/reference/java/lang/ClassValue#get(java.lang.Class<?>)` finds no associated value.

If this method throws an exception, the initiating `get` call will not attempt to associate a value, and may terminate by returning the associated value if it exists, or by propagating that exception otherwise.

Otherwise, the value is computed and returned. An attempt to associate the return value happens, with one of the following outcomes:

- The associated value is present; it is returned and no association is done.
- The most recent `https://developer.android.com/reference/java/lang/ClassValue#remove(java.lang.Class<?>)` call, if it exists, does not happen-before (JLS {@jls 17.4.5}) the finish of the `computeValue` that computed the value to associate. A new invocation to `computeValue`, which that `remove` call happens-before, will re-establish this happens-before relationship.
- Otherwise, this value is successfully associated and returned.

<br />

<br />

##### API Note:

- A `computeValue` call may, due to class loading or other circumstances, recursively call `get` or `remove` for the same `type`. The recursive `get`, if the recursion stops, successfully finishes and this initiating `get` observes the associated value from recursion. The recursive `remove` is no-op, since being on the same thread, the `remove` already happens-before the finish of this `computeValue`; the result from this `computeValue` still may be associated.

| Parameters ||
|---|---|
| `type` | `Class`: the `Class` to associate a value to <br /> |

| Returns ||
|---|---|
| `T` | the newly computed value to associate <br /> |

**See also:**

- `https://developer.android.com/reference/java/lang/ClassValue#get(java.lang.Class<?>)`
- `https://developer.android.com/reference/java/lang/ClassValue#remove(java.lang.Class<?>)`