+ 4
What's is the difference between "Static" and "Constant" variable ?
8 Respostas
+ 6
Static is an access modifier which defines that a member is a class' property rather than an instance's property, this means, the property (method or value) can be utilized without a need for creating an instance (object) of the said class.
Constant, as the name implies, is a way to define a constant (non modifiable) value, in most programming languages constants value needs to be assigned where the constant was declared, with an exception of C++ with its constructor initializer (probably such feature is available in other language I am not aware of).
Hth, cmiiw
+ 5
constant variable value cannot be changed throughout the program, you cannot alter it's value ones assigned.
static is variable which is shared among all, if you change its value ij function it will reflect to it's original value.
+ 5
yes. I think my statement was wrong. Both of them can be accessed out of class by using the dot operator (ex. `Test.a`). It looks like their only main difference is that the const is a "read only" variable while static makes the variable "global" within the instances of a class.
I just remember an example of a variable that can be both const and static, the Math.PI in Java. Math.PI is const that is you can't change its value. At the same time, it is also static that is why you don't have to declare an instance of Math class. You don't need to write `Math x = new Math()` and then write `x.PI` instead you access it directly as `Math.PI`, although it is still possible to do that (but in Math, you can't because the Math class is static).
+ 3
also, in C++ there is a special space in memory for const qualified variables.
+ 3
Can we call Const outside of the class
class Test {
public const int a = 10;
}
Main Method ----
Console.Write(Test.a);
+ 2
static is a mode for varraible and methods but constant is a mode for contains of varraibls it's make a sens for you ???
+ 2
Const and Static also same because everytime creating an instance Const variable is only one copy and Static variable is only one copy
Only Difference is Static variable can be modified and Const can't be modified
and Const variable belongs to the class.
0
const variables can be only accessed within its scope, while static variables can be accessed outside of the class as its property