1# Constraints on Access Modifiers of Custom Component Member Variables 2 3 4In ArkTS, use of the access modifiers – **private**, **public**, and **protected** – for custom component member variables must comply with the constraints described in this topic. Build errors will be reported for any incompliance. 5 6 7> **NOTE** 8> 9> The constraints on access modifiers of custom component member variables are supported since API version 12. 10 11 12## Constraints 13 14- For regular variables (which do not involve re-rendering) and variables decorated by \@State, \@Prop, \@Provide, or \@BuilderParam, when declared as **private**, value assignment is not allowed during custom component construction. 15 16- For variables decorated by \@StorageLink, \@StorageProp, \@LocalStorageLink, \@LocalStorageProp, or \@Consume, **public** access is not allowed. 17 18- For variables decorated by \@Link or \@ObjectLink, **private** access is not allowed. 19 20- Because structs do not support inheritance, none of the preceding variables can be declared as **protected**. 21 22- The regular variables (which do not involve re-rendering) and variables decorated by \@State, \@Prop, \@Provide, or \@BuilderParam in custom components cannot be decorated by both \@Require and **private**. 23 24 25## Examples of Incorrect Usage 26 271. If a member variable is modified by both the **private** access modifier and the \@State, \@Prop, \@Provide, or \@BuilderParam decorator, a build error is reported. 28 29```ts 30@Entry 31@Component 32struct AccessRestrictions { 33 @Builder buildTest() { 34 Text("Parent builder") 35 } 36 build() { 37 Column() { 38 ComponentsChild({state_value: "Hello", prop_value: "Hello", provide_value: "Hello", builder_value: this.buildTest, regular_value: "Hello"}) 39 } 40 .width('100%') 41 } 42} 43 44@Component 45struct ComponentsChild { 46 @State private state_value: string = "Hello"; 47 @Prop private prop_value: string = "Hello"; 48 @Provide private provide_value: string = "Hello"; 49 @BuilderParam private builder_value: () => void = this.buildTest; 50 private regular_value: string = "Hello"; 51 @Builder buildTest() { 52 Text("Child builder") 53 } 54 build() { 55 Column() { 56 Text("Hello") 57 .fontSize(50) 58 .fontWeight(FontWeight.Bold) 59 } 60 } 61} 62``` 63 64The following are some build error examples: 65 66```ts 67Property 'state_value' is private and can not be initialized through the component constructor. 68Property 'prop_value' is private and can not be initialized through the component constructor. 69Property 'provide_value' is private and can not be initialized through the component constructor. 70Property 'builder_value' is private and can not be initialized through the component constructor. 71Property 'regular_value' is private and can not be initialized through the component constructor. 72``` 73 742. If a member variable is modified by both the **public** access modifier and the \@StorageLink, \@StorageProp, \@LocalStorageLink, \@LocalStorageProp, or \@Consume decorator, a build error is reported. 75 76```ts 77@Entry 78@Component 79struct AccessRestrictions { 80 @Provide consume_value: string = "Hello"; 81 build() { 82 Column() { 83 ComponentChild() 84 } 85 .width('100%') 86 } 87} 88 89@Component 90struct ComponentChild { 91 @LocalStorageProp("sessionLocalProp") public local_prop_value: string = "Hello"; 92 @LocalStorageLink("sessionLocalLink") public local_link_value: string = "Hello"; 93 @StorageProp("sessionProp") public storage_prop_value: string = "Hello"; 94 @StorageLink("sessionLink") public storage_link_value: string = "Hello"; 95 @Consume public consume_value: string; 96 build() { 97 Column() { 98 Text("Hello") 99 .fontSize(50) 100 .fontWeight(FontWeight.Bold) 101 } 102 } 103} 104``` 105 106The following are some build error examples: 107 108```ts 109Property 'local_prop_value' can not be decorated with both @LocalStorageProp and public. 110Property 'local_link_value' can not be decorated with both @LocalStorageLink and public. 111Property 'storage_prop_value' can not be decorated with both @StorageProp and public. 112Property 'storage_link_value' can not be decorated with both @StorageLink and public. 113Property 'consume_value' can not be decorated with both @Consume and public. 114``` 115 1163. If a member variable is modified by both the **private** access modifier and the \@Link or \@ObjectLink decorator, a build error is reported. 117 118```ts 119@Entry 120@Component 121struct AccessRestrictions { 122 @State link_value: string = "Hello"; 123 @State objectLink_value: ComponentObj = new ComponentObj(); 124 build() { 125 Column() { 126 ComponentChild({link_value: this.link_value, objectLink_value: this.objectLink_value}) 127 } 128 .width('100%') 129 } 130} 131 132@Observed 133class ComponentObj { 134 count: number = 0; 135} 136@Component 137struct ComponentChild { 138 @Link private link_value: string; 139 @ObjectLink private objectLink_value: ComponentObj; 140 build() { 141 Column() { 142 Text("Hello") 143 .fontSize(50) 144 .fontWeight(FontWeight.Bold) 145 } 146 } 147} 148``` 149 150The following are some build error examples: 151 152```ts 153Property 'link_value' can not be decorated with both @Link and private. 154Property 'objectLink_value' can not be decorated with both @ObjectLink and private. 155``` 156 1574. If a member variable is modified by the **protected** access modifier, a build error is reported. 158 159```ts 160@Entry 161@Component 162struct AccessRestrictions { 163 build() { 164 Column() { 165 ComponentChild({regular_value: "Hello"}) 166 } 167 .width('100%') 168 } 169} 170 171@Component 172struct ComponentChild { 173 protected regular_value: string = "Hello"; 174 build() { 175 Column() { 176 Text("Hello") 177 .fontSize(50) 178 .fontWeight(FontWeight.Bold) 179 } 180 } 181} 182``` 183 184The following are some build error examples: 185 186```ts 187The member attributes of a struct can not be protected. 188``` 189 1905. If a member variable is modified by the **private** access modifier, the \@Require decorator, and the \@State, \@Prop, \@Provide, or \@BuilderParam decorator, a build error is reported. 191 192```ts 193@Entry 194@Component 195struct AccessRestrictions { 196 build() { 197 Column() { 198 ComponentChild({prop_value: "Hello"}) 199 } 200 .width('100%') 201 } 202} 203@Component 204struct ComponentChild { 205 @Require @Prop private prop_value: string = "Hello"; 206 build() { 207 Column() { 208 Text("Hello") 209 .fontSize(50) 210 .fontWeight(FontWeight.Bold) 211 } 212 } 213} 214``` 215 216The following are some build error examples: 217 218```ts 219Property 'prop_value' can not be decorated with both @Require and private. 220Property 'prop_value' is private and can not be initialized through the component constructor. 221``` 222