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 decorated by both the **private** access modifier and the \@State, \@Prop, \@Provide, or \@BuilderParam decorator, and is initialized through the parent component, a build error is reported.
28
29```ts
30@Entry
31@Component
32struct AccessRestrictions {
33  @Builder
34  buildTest() {
35    Text("Parent builder")
36  }
37
38  build() {
39    Column() {
40      ComponentsChild({
41        state_value: "Hello",
42        prop_value: "Hello",
43        provide_value: "Hello",
44        builder_value: this.buildTest,
45        regular_value: "Hello"
46      })
47    }
48    .width('100%')
49  }
50}
51
52@Component
53struct ComponentsChild {
54  @State private state_value: string = "Hello";
55  @Prop private prop_value: string = "Hello";
56  @Provide private provide_value: string = "Hello";
57  @BuilderParam private builder_value: () => void = this.buildTest;
58  private regular_value: string = "Hello";
59
60  @Builder
61  buildTest() {
62    Text("Child builder")
63  }
64
65  build() {
66    Column() {
67      Text("Hello")
68        .fontSize(50)
69        .fontWeight(FontWeight.Bold)
70    }
71  }
72}
73```
74
75The following are some build error examples:
76
77```ts
78Property 'state_value' is private and can not be initialized through the component constructor.
79Property 'prop_value' is private and can not be initialized through the component constructor.
80Property 'provide_value' is private and can not be initialized through the component constructor.
81Property 'builder_value' is private and can not be initialized through the component constructor.
82Property 'regular_value' is private and can not be initialized through the component constructor.
83```
84
852. If a member variable is decorated by both the **public** access modifier and the \@StorageLink, \@StorageProp, \@LocalStorageLink, \@LocalStorageProp, or \@Consume decorator, and is initialized through the parent component, a build error is reported.
86
87```ts
88@Entry
89@Component
90struct AccessRestrictions {
91  @Provide consume_value: string = "Hello";
92  build() {
93    Column() {
94      ComponentChild()
95    }
96    .width('100%')
97  }
98}
99
100@Component
101struct ComponentChild {
102  @LocalStorageProp("sessionLocalProp") public local_prop_value: string = "Hello";
103  @LocalStorageLink("sessionLocalLink") public local_link_value: string = "Hello";
104  @StorageProp("sessionProp") public storage_prop_value: string = "Hello";
105  @StorageLink("sessionLink") public storage_link_value: string = "Hello";
106  @Consume public consume_value: string;
107  build() {
108    Column() {
109      Text("Hello")
110        .fontSize(50)
111        .fontWeight(FontWeight.Bold)
112    }
113  }
114}
115```
116
117The following are some build error examples:
118
119```ts
120Property 'local_prop_value' can not be decorated with both @LocalStorageProp and public.
121Property 'local_link_value' can not be decorated with both @LocalStorageLink and public.
122Property 'storage_prop_value' can not be decorated with both @StorageProp and public.
123Property 'storage_link_value' can not be decorated with both @StorageLink and public.
124Property 'consume_value' can not be decorated with both @Consume and public.
125```
126
1273. If a member variable is decorated by both the **private** access modifier and the \@Link/ or \@ObjectLink decorator, and is initialized through the parent component, a build error is reported.
128
129```ts
130@Entry
131@Component
132struct AccessRestrictions {
133  @State link_value: string = "Hello";
134  @State objectLink_value: ComponentObj = new ComponentObj();
135  build() {
136    Column() {
137      ComponentChild({link_value: this.link_value, objectLink_value: this.objectLink_value})
138    }
139    .width('100%')
140  }
141}
142
143@Observed
144class ComponentObj {
145  count: number = 0;
146}
147@Component
148struct ComponentChild {
149  @Link private link_value: string;
150  @ObjectLink private objectLink_value: ComponentObj;
151  build() {
152    Column() {
153      Text("Hello")
154        .fontSize(50)
155        .fontWeight(FontWeight.Bold)
156    }
157  }
158}
159```
160
161The following are some build error examples:
162
163```ts
164Property 'link_value' can not be decorated with both @Link and private.
165Property 'objectLink_value' can not be decorated with both @ObjectLink and private.
166```
167
1684. If a member variable is decorated by the **protected** access modifier and is initialized through the parent component, a build error is reported.
169
170```ts
171@Entry
172@Component
173struct AccessRestrictions {
174  build() {
175    Column() {
176      ComponentChild({regular_value: "Hello"})
177    }
178    .width('100%')
179  }
180}
181
182@Component
183struct ComponentChild {
184  protected regular_value: string = "Hello";
185  build() {
186    Column() {
187      Text("Hello")
188        .fontSize(50)
189        .fontWeight(FontWeight.Bold)
190    }
191  }
192}
193```
194
195The following are some build error examples:
196
197```ts
198The member attributes of a struct can not be protected.
199```
200
2015. If a member variable is decorated by both the **private** access modifier and the \@Require, \@State, \@Prop, \@Provide, or \@BuilderParam decorator, and is initialized through the parent component, a build error is reported.
202
203```ts
204@Entry
205@Component
206struct AccessRestrictions {
207  build() {
208    Column() {
209      ComponentChild({prop_value: "Hello"})
210    }
211    .width('100%')
212  }
213}
214@Component
215struct ComponentChild {
216  @Require @Prop private prop_value: string = "Hello";
217  build() {
218    Column() {
219      Text("Hello")
220        .fontSize(50)
221        .fontWeight(FontWeight.Bold)
222    }
223  }
224}
225```
226
227The following are some build error examples:
228
229```ts
230Property 'prop_value' can not be decorated with both @Require and private.
231Property 'prop_value' is private and can not be initialized through the component constructor.
232```
233