---
title: Forced Decision methods for the Java SDK
slug: feature-experimentation/docs/forced-decision-methods-java
docTags: 
createdAt: 2026-08-05T20:39:52.000Z
---

These methods help test and debug various flows of your client applications by forcing users into a specific variation.

The Java SDK checks forced decisions before making any decisions. If a matching item is found for the requested flag, the Java SDK returns the forced decision immediately ([audience conditions](docId\:UDBvr5a8WHNkyOqzPhOr9) and traffic allocations are ignored) before making normal decisions.

The Java SDK checks for forced decisions at the start of each decision process. If a matching forced decision is found, it returns the decision immediately.

- **Flag-to-Decision** – The SDK checks at the start of any decide call for the given flag.
- **Experiment-Rule-to-Decision** – The SDK checks at the start of the decision for the given experiment rule of the flag key.
- **Delivery-Rule-to-Decision** – The SDK checks at the start of the decision for the given delivery rule of the flag key.

:::hint{type="danger"}
**Warning**
You must associate your variation(s) to a [flag rule](docId:-yq2-thNRv7-SgwCXpG90) before calling any Forced Decision methods.
:::

On forced decisions, the Java SDK fires impression events and notifications just like other normal decisions (unless disabled by the [disableDecisionEvent](docId:6ZSh7uRg7JxlrHwft2Ogf) option).

:::hint{type="info"}
**Note**
These forced decisions are not persistent and are cleared when the [OptimizelyUserContext](docId\:gm_UYsgiYClOVsYuAnlBX) is re-initialized.
:::

For information about each method, click on the method name.

- [setForcedDecision](docId:4Ylpzx_4F-bPM950pxDQP)
- [getForcedDecision](docId:4Ylpzx_4F-bPM950pxDQP)
- [removeForcedDecision](docId:4Ylpzx_4F-bPM950pxDQP)
- [removeAllForcedDecisions](docId:4Ylpzx_4F-bPM950pxDQP)

### OptimizelyDecisionContext

```java
public class OptimizelyDecisionContext {
   public OptimizelyDecisionContext(@Nonnull String flagKey, @Nullable String ruleKey);
  
   public String getFlagKey();
  
   public String getRuleKey();
}
```

### OptimizelyForcedDecision

```java
public class OptimizelyForcedDecision {
   public OptimizelyForcedDecision(@Nonnull String variationKey);
  
   public String getVariationKey();
}
```

## Set forced decision method - setForcedDecision()

### Version

3.10.0

### Description

Sets a forced decision (`variationKey`) for a given `OptimizelyDecisionContext`.

### Parameters

This table lists the required and optional parameters for the Java SDK.

| Parameter                  | Type  | Description                                                                                                                                  |
| -------------------------- | ----- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\*   | Class | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to set. |
| **decision**\ \*required\* | Class | An instance of \`OptimizelyForcedDecision\` with the required \`variationKey\` for the forced decision you want to set.                      |

### Returns

A boolean value that indicates if setting the forced decision (`variationKey`) was completed.

### Example

See the [full Java SDK code example](docId:4Ylpzx_4F-bPM950pxDQP).

## Get forced decision method - getForcedDecision()

### Version

3.10.0

### Description

Returns the forced decision (`variationKey`) for a given `OptimizelyDecisionContext`. Returns the `OptimizelyForcedDecision` instance or null if there is no matching item.

### Parameters

This table lists the required and optional parameters for the Java SDK.

| Parameter                | Type  | Description                                                                                                                                  |
| ------------------------ | ----- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\* | Class | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to get. |

### Returns

A forced decision `OptimizelyForcedDecision` instance for the context or null if there is no matching item.

### Example

See the [full Java SDK code example](docId:4Ylpzx_4F-bPM950pxDQP).

## Remove forced decision method - removeForcedDecision()

### Version

3.10.0

### Description

Removes the forced decision (`variationKey`) for a given `OptimizelyDecisionContext`.

### Parameters

This table lists the required and optional parameters for the Java SDK.

| Parameters               | Type  | Description                                                                                                                                     |
| ------------------------ | ----- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\* | Class | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to remove. |

### Returns

A success/failure boolean status if the forced decision (`variationKey`) was removed.

### Example

See the [full Java SDK code example](docId:4Ylpzx_4F-bPM950pxDQP).

## Remove all forced decisions method - removeAllForcedDecisions()

### Version

3.10.0

### Description

Removes all forced decisions (`variationKey`) for the user context.

### Parameters

This table lists the required and optional parameters for the Java SDK.

| Parameters | Type | Description |
| ---------- | ---- | ----------- |
| None       | N/A  | N/A         |

### Returns

A success/failure boolean status.

### Example

See the [full Java SDK code example](docId:4Ylpzx_4F-bPM950pxDQP).

## Full Code Example

```java
/* Create the Optimizely instance and provide the sdk-key */
Optimizely optimizely = OptimizelyFactory.newDefaultInstance("sdk-key");
/* Create the OptimizelyUserContext, passing in the UserId and Attributes */
OptimizelyUserContext user = optimizely.createUserContext("user-id", Collections.emptyMap());

OptimizelyDecisionContext flagContext = new OptimizelyDecisionContext("flag-1", null);
OptimizelyDecisionContext flagAndABTestContext = new OptimizelyDecisionContext("flag-1", "ab-test-1");
OptimizelyDecisionContext flagAndDeliveryRuleContext = new OptimizelyDecisionContext("flag-1", "delivery-1");
OptimizelyForcedDecision variationAForcedDecision = new OptimizelyForcedDecision("variation-a");
OptimizelyForcedDecision variationBForcedDecision = new OptimizelyForcedDecision("variation-b");
OptimizelyForcedDecision variationOnForcedDecision = new OptimizelyForcedDecision("on");
        
// set a forced decision for a flag
Boolean success = user.setForcedDecision(flagContext, variationAForcedDecision);
OptimizelyDecision decision = user.decide("flag-1");

// set a forced decision for an ab-test rule
success = user.setForcedDecision(flagAndABTestContext, variationBForcedDecision);
decision = user.decide("flag-1");

// set a forced variation for a delivery rule
success = user.setForcedDecision(flagAndDeliveryRuleContext, variationOnForcedDecision);
decision = user.decide("flag-1");

// get forced variations
OptimizelyForcedDecision forcedDecision = user.getForcedDecision(flagContext);
System.out.println("[ForcedDecision] variationKey = " + forcedDecision.getVariationKey()); 

// remove forced variations
success = user.removeForcedDecision(flagAndABTestContext);
success = user.removeAllForcedDecisions();
```

## See Also

[OptimizelyUserContext](docId\:gm_UYsgiYClOVsYuAnlBX)
