---
sidebar_position: 40
title: Java
description: How to use the OpenFeature JAVA SDK
---





import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import versions from '@site/static/sdk-versions.json';
import CodeBlock from '@theme/CodeBlock';

# JAVA SDK usage
[![Maven Central Version](https://img.shields.io/maven-central/v/dev.openfeature.contrib.providers/go-feature-flag?color=blue&style=flat-square)](https://search.maven.org/artifact/dev.openfeature.contrib.providers/go-feature-flag)

## Install dependencies

The first thing we will do is install the **Open Feature SDK** and the **GO Feature Flag provider**.


<Tabs groupId="code">
  <TabItem value="maven" label="maven">
    <CodeBlock
      language="xml"
      showLineNumbers>
      {
        `<dependency>
    <groupId>dev.openfeature</groupId>
    <artifactId>sdk</artifactId>
    <artifactId>${versions.maven.sdk}</artifactId>
</dependency>
<dependency>
    <groupId>dev.openfeature.contrib.providers</groupId>
    <artifactId>go-feature-flag</artifactId>
    <version>${versions.maven.providerJava}</version>
</dependency>`
      }
    </CodeBlock>

  </TabItem>
  <TabItem value="gradle" label="gradle">
    <CodeBlock
      language="toml"
      showLineNumbers>
      {
        `implementation group: 'dev.openfeature', name: 'javasdk', version: '${versions.maven.sdk}'
implementation group: 'dev.openfeature.contrib.providers', name: 'go-feature-flag', version: '${versions.maven.providerJava}'`}
    </CodeBlock>

  </TabItem>
</Tabs>


## Initialize your Open Feature client
To evaluate the flag you need to have an Open Feature configured in your app.
This code block shows you how you can create a client that you can use in your application.

<Tabs groupId="code">
  <TabItem value="java" label="java">

    ```java
    import dev.openfeature.contrib.providers.gofeatureflag.GoFeatureFlagProvider;
    import dev.openfeature.contrib.providers.gofeatureflag.GoFeatureFlagProviderOptions;
    import dev.openfeature.sdk.EvaluationContext;
    import dev.openfeature.sdk.MutableContext;
    import dev.openfeature.sdk.OpenFeatureAPI;

    // ...

    GoFeatureFlagProviderOptions options =
    GoFeatureFlagProviderOptions.builder().endpoint("http://localhost:1031/").build();
    GoFeatureFlagProvider provider = new GoFeatureFlagProvider(options);

    OpenFeatureAPI.getInstance().setProvider(provider);
    OpenFeatureAPI api = OpenFeatureAPI.getInstance();
    Client featureFlagClient = api.getClient();
    ```

  </TabItem>
  <TabItem value="kotlin" label="Kotlin">

    ```kotlin
    import dev.openfeature.contrib.providers.gofeatureflag.GoFeatureFlagProvider
    import dev.openfeature.contrib.providers.gofeatureflag.GoFeatureFlagProviderOptions
    import dev.openfeature.sdk.EvaluationContext
    import dev.openfeature.sdk.MutableContext
    import dev.openfeature.sdk.OpenFeatureAPI

    // ...

    val options = GoFeatureFlagProviderOptions.builder().endpoint("http://localhost:1031/").build()
    val provider = GoFeatureFlagProvider(options)

    OpenFeatureAPI.getInstance().provider = provider
    // wait for the provider to be ready
    val api = OpenFeatureAPI.getInstance()
    val featureFlagClient = api.client
    ```

  </TabItem>
</Tabs>


## Evaluate your flag

This code block explain how you can create an `EvaluationContext` and use it to evaluate your flag.

:::note
In this example we are evaluating a `boolean` flag, but other types are available.

**Refer to the [Open Feature documentation](https://docs.openfeature.dev/docs/reference/concepts/evaluation-api#basic-evaluation) to know more about it.**
:::

<Tabs groupId="code">
  <TabItem value="java" label="java">

    ```java
    // Context of your flag evaluation.
    // With GO Feature Flag you MUST have a targetingKey that is a unique identifier of the user.
    EvaluationContext userContext = new MutableContext("1d1b9238-2591-4a47-94cf-d2bc080892f1")
      .add("firstname", "john")
      .add("lastname", "doe")
      .add("email","john.doe@gofeatureflag.org")
      .add("admin", true)
      .add("anonymous", false);

    Boolean adminFlag = featureFlagClient.getBooleanValue("flag-only-for-admin", false, userContext);
    if (adminFlag) {
      // flag "flag-only-for-admin" is true for the user
    } else {
      // flag "flag-only-for-admin" is false for the user
    }
    ```

  </TabItem>
  <TabItem value="kotlin" label="Kotlin">

    ```kotlin
    // Context of your flag evaluation.
    // With GO Feature Flag you MUST have a targetingKey that is a unique identifier of the user.
    val userContext: EvaluationContext = MutableContext("1d1b9238-2591-4a47-94cf-d2bc080892f1")
      .add("firstname", "john")
      .add("lastname", "doe")
      .add("email", "john.doe@gofeatureflag.org")
      .add("admin", true)
      .add("anonymous", false)

    val adminFlag = featureFlagClient.getBooleanValue("bool_targeting_match", false, userContext)
    if (adminFlag) {
      // flag "flag-only-for-admin" is true for the user
    } else {
      // flag "flag-only-for-admin" is false for the user
    }
    ```

  </TabItem>
</Tabs>

## Breaking changes

### 0.4.0 - Cache Implementation Change: Guava to Caffeine

In this release, we have updated the cache implementation from Guava to Caffeine. This change was made because Caffeine is now the recommended caching solution by the maintainers of Guava due to its performance improvements and enhanced features.

Because of this, the cache configuration on `GoFeatureFlagProviderOptions` that used Guava's `CacheBuilder` is now handled by `Caffeine`.

#### How to migrate

Configuration cache with Guava used to be like this:

```java
import com.google.common.cache.CacheBuilder;
// ...
CacheBuilder guavaCacheBuilder = CacheBuilder.newBuilder()
  .initialCapacity(100)
  .maximumSize(2000);

FeatureProvider provider = new GoFeatureFlagProvider(
  GoFeatureFlagProviderOptions
  .builder()
  .endpoint("https://my-gofeatureflag-instance.org")
  .cacheBuilder(guavaCacheBuilder)
  .build());

OpenFeatureAPI.getInstance().setProviderAndWait(provider);

// ...
```

Now with Caffeine it should be like this:

```java
import com.github.benmanes.caffeine.cache.Caffeine;
// ...
Caffeine caffeineCacheConfig = Caffeine.newBuilder()
  .initialCapacity(100)
  .maximumSize(2000);

FeatureProvider provider = new GoFeatureFlagProvider(
  GoFeatureFlagProviderOptions
  .builder()
  .endpoint("https://my-gofeatureflag-instance.org")
  .cacheConfig(caffeineCacheConfig)
  .build());

OpenFeatureAPI.getInstance().setProviderAndWait(provider);

// ...
```

For a complete list of customizations  options available in Caffeine, please refer to the [Caffeine documentation](https://github.com/ben-manes/caffeine/wiki) for more details.

## Contribute to the provider
You can find the source of the provider in the [`open-feature/java-sdk-contrib`](https://github.com/open-feature/java-sdk-contrib/tree/main/providers/go-feature-flag) repository.