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