+ 4

What's is the difference between "Static" and "Constant" variable ?

9th Sep 2018, 3:27 PM
Real Gutch
Real Gutch - avatar
8 Answers
+ 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
9th Sep 2018, 4:08 PM
Ipang
+ 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.
9th Sep 2018, 4:00 PM
Roland Mendonca
Roland Mendonca - avatar
+ 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).
10th Sep 2018, 5:06 AM
Kulisap
Kulisap - avatar
+ 3
also, in C++ there is a special space in memory for const qualified variables.
9th Sep 2018, 5:21 PM
Bebida Roja
Bebida Roja - avatar
+ 3
Can we call Const outside of the class class Test { public const int a = 10; } Main Method ---- Console.Write(Test.a);
10th Sep 2018, 2:14 AM
Real Gutch
Real Gutch - avatar
+ 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 ???
9th Sep 2018, 10:29 PM
Mehdi Chellak
Mehdi Chellak - avatar
+ 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.
10th Sep 2018, 1:27 AM
Real Gutch
Real Gutch - avatar
0
const variables can be only accessed within its scope, while static variables can be accessed outside of the class as its property
10th Sep 2018, 1:52 AM
Kulisap
Kulisap - avatar