Why do we need to declare our variables, members or classes as static or const ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why do we need to declare our variables, members or classes as static or const ?

1st Jul 2017, 5:14 PM
Haider Ali
Haider Ali - avatar
3 Answers
+ 5
for memory consumption
1st Jul 2017, 5:30 PM
Sergio Araya Villalobos
Sergio Araya Villalobos - avatar
+ 3
const is when u decided not to change the value any further u access static variable when u are definitely sure there is only one unique value for that variable in a class. also u dont need to declare a new instance if u need to access the static variable
1st Jul 2017, 5:30 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
0
const is used to make sure that the value of that property won't change. For instance, the value of Pi should never be changed throughout your program. Using const ensures that its value isn't inadvertently changed by some other part of your program or some other programmer on your team etc. A static property is a property that belongs to the class and its value should be the same for every instance of that class that is created. For instance, let's say you had a variable named count that kept track of the number of instances of the class that were created and still existed. You would want that number to be exactly the same for all instances of that class in order for the returned value to be correct.
1st Jul 2017, 5:34 PM
ChaoticDawg
ChaoticDawg - avatar