0
Why do we need to declare our variables, members or classes as static or const ?
3 ответов
+ 5
for memory consumption
+ 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
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.