Stay organized with collections
Save and categorize content based on your preferences.
@Target(allowedTargets = [AnnotationTarget.FIELD, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER])
@Retention(value = AnnotationRetention.BINARY)
public annotation GuardedBy
Denotes that the annotated method or field can only be accessed when holding the referenced lock.
Example:
final Object objectLock = new Object();
@GuardedBy("objectLock")
volatile Object object;
Object getObject() {
synchronized (objectLock) {
if (object == null) {
object = new Object();
}
}
return object;
}
Summary
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-07-17 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-07-17 UTC."],[],[],null,["# GuardedBy\n=========\n\nArtifact: [androidx.annotation:annotation](/jetpack/androidx/releases/annotation) \n[View Source](https://cs.android.com/search?q=file:androidx/annotation/GuardedBy.kt+class:androidx.annotation.GuardedBy) \nAdded in [1.0.0](/jetpack/androidx/releases/annotation#1.0.0)\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/androidx/annotation/GuardedBy \"View this page in Kotlin\") \\|Java\n\n\n```\n@Target(allowedTargets = [AnnotationTarget.FIELD, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER])\n@Retention(value = AnnotationRetention.BINARY)\npublic annotation GuardedBy\n```\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nDenotes that the annotated method or field can only be accessed when holding the referenced lock.\n\nExample: \n\n```transact-sql\nfinal Object objectLock = new Object();\n\n@GuardedBy(\"objectLock\")\nvolatile Object object;\n\nObject getObject() {\n synchronized (objectLock) {\n if (object == null) {\n object = new Object();\n }\n }\n return object;\n}\n```\n\nSummary\n-------\n\n| ### Public constructors |\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [GuardedBy](/reference/androidx/annotation/GuardedBy#GuardedBy(kotlin.String))`(@`[NonNull](/reference/androidx/annotation/NonNull)` `[String](https://developer.android.com/reference/java/lang/String.html)` value)` |\n\n| ### Public methods |\n|--------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------|\n| `final @`[NonNull](/reference/androidx/annotation/NonNull)` `[String](https://developer.android.com/reference/java/lang/String.html) | [getValue](/reference/androidx/annotation/GuardedBy#getValue())`()` |\n\nPublic constructors\n-------------------\n\n### GuardedBy\n\n```\npublic GuardedBy(@NonNull String value)\n``` \n\nPublic methods\n--------------\n\n### getValue\n\n```\npublic final @NonNull String getValue()\n```"]]