Added in [API level 30](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/util/CloseGuard#pubctors) \| [Methods](https://developer.android.com/reference/android/util/CloseGuard#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/util/CloseGuard#inhmethods)

# CloseGuard

*** ** * ** ***

` public final class CloseGuard `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.util.CloseGuard |

<br />

*** ** * ** ***

CloseGuard is a mechanism for flagging implicit finalizer cleanup of resources that should have been cleaned up by explicit close methods (aka "explicit termination methods" in Effective Java).

A simple example:

    class Foo {

          private final CloseGuard guard = new CloseGuard();

          ...

          public Foo() {
              ...;
              guard.open("cleanup");
          }

          public void cleanup() {
             guard.close();
             ...;
             if (Build.VERSION.SDK_INT >= 28) {
                 Reference.reachabilityFence(this);
             }
             // For full correctness in the absence of a close() call, other methods may also need
             // reachabilityFence() calls.
          }

          protected void finalize() throws Throwable {
              try {
                  // Note that guard could be null if the constructor threw.
                  if (guard != null) {
                      guard.warnIfOpen();
                  }
                  cleanup();
              } finally {
                  super.finalize();
              }
          }
      }

In usage where the resource to be explicitly cleaned up is allocated after object construction, CloseGuard protection can be deferred. For example:

    class Bar {

          private final CloseGuard guard = new CloseGuard();

          ...

          public Bar() {
              ...;
          }

          public void connect() {
             ...;
             guard.open("cleanup");
          }

          public void cleanup() {
             guard.close();
             ...;
             if (Build.VERSION.SDK_INT >= 28) {
                 Reference.reachabilityFence(this);
             }
             // For full correctness in the absence of a close() call, other methods may also need
             // reachabilityFence() calls.
          }

          protected void finalize() throws Throwable {
              try {
                  // Note that guard could be null if the constructor threw.
                  if (guard != null) {
                      guard.warnIfOpen();
                  }
                  cleanup();
              } finally {
                  super.finalize();
              }
          }
      }

When used in a constructor, calls to `open` should occur at the end of the constructor since an exception that would cause abrupt termination of the constructor will mean that the user will not have a reference to the object to cleanup explicitly. When used in a method, the call to `open` should occur just after resource acquisition.

<br />

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/util/CloseGuard#CloseGuard()() ` Constructs a new CloseGuard instance. |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/util/CloseGuard#close()() ` Marks this CloseGuard instance as closed to avoid warnings on finalization. |
| ` void` | ` https://developer.android.com/reference/android/util/CloseGuard#open(java.lang.String)(https://developer.android.com/reference/java/lang/String closeMethodName) ` Initializes the instance with a warning that the caller should have explicitly called the `closeMethodName` method instead of relying on finalization. |
| ` void` | ` https://developer.android.com/reference/android/util/CloseGuard#warnIfOpen()() ` Logs a warning if the caller did not properly cleanup by calling an explicit close method before finalization. |

### 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*.

## Public constructors

### CloseGuard

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

```
public CloseGuard ()
```

Constructs a new CloseGuard instance. `https://developer.android.com/reference/android/util/CloseGuard#open(java.lang.String)` can be used to set up the instance to warn on failure to close.

<br />

## Public methods

### close

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

```
public void close ()
```

Marks this CloseGuard instance as closed to avoid warnings on finalization.

<br />

### open

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

```
public void open (String closeMethodName)
```

Initializes the instance with a warning that the caller should have explicitly called the `closeMethodName` method instead of relying on finalization.

<br />

| Parameters ||
|---|---|
| `closeMethodName` | `String`: non-null name of explicit termination method. Printed by warnIfOpen. <br /> |

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

### warnIfOpen

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

```
public void warnIfOpen ()
```

Logs a warning if the caller did not properly cleanup by calling an explicit close method before finalization.

<br />