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\nKotlin \\|[Java](/reference/androidx/annotation/GuardedBy \"View this page in Java\")\nCommon/AllAndroid/JVMNative/C/iOS\n - Cmn \n\n ```\n @Target(allowedTargets = [AnnotationTarget.FIELD, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER])\n @Retention(value = AnnotationRetention.BINARY)\n annotation GuardedBy\n ```\n\n*** ** * ** ***\n\nDenotes that the annotated method or field can only be accessed when holding the referenced lock.\n\nExample: \n\n```kotlin\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/kotlin/androidx/annotation/GuardedBy#GuardedBy(kotlin.String))`(value: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`)` | Cmn |\n\n| ### Public properties |\n|-----------------------------------------------------------------------------------|------------------------------------------------------------------|-----|\n| [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html) | [value](/reference/kotlin/androidx/annotation/GuardedBy#value()) | Cmn |\n\nPublic constructors\n-------------------\n\n### GuardedBy\n\nCmn \n\n```\nGuardedBy(value: String)\n```\n\nPublic properties\n-----------------\n\n### value\n\nCmn \n\n```\nval value: String\n```"]]