Skip to content

Commit c1e8b56

Browse files
author
Aman Gupta
committed
constants and finals
1 parent 230bc07 commit c1e8b56

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

constants_and_finals.dart

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
void main() {
2+
/*final: can only be set once and is initialized when accessed
3+
- only consume memory if used
4+
constant: implicitly final but it is compile-time constant
5+
i.e. it is initialized during compilation - it will consume memory no matter
6+
if you're using it or not
7+
NOTE: Instance var can be final but cannot be const.
8+
- If you want a const at class level then make it static const
9+
*/
10+
11+
final cityName = "Delhi";
12+
//String is optional
13+
14+
const double PI = 3.14;
15+
16+
Circle circle = new Circle();
17+
print(circle.color);
18+
}
19+
20+
class Circle {
21+
final color = "Red";
22+
static const PI = 3.14;
23+
}

0 commit comments

Comments
 (0)