Questions tagged [constants]

Constants in programming are definitions whose value is fixed throughout a program's execution. Literals in most languages are constants, for example. In referentially transparent programming styles, all definitions are constant. A const-qualified data storage area (object, field, variable, parameter) is one that "never changes", thus allowing extra code generator optimizations and additional static checking of program correctness.

In computer science, const-correctness is the form of program correctness that deals with the proper declaration of objects as mutable or immutable. The term is mostly used in a C or C++ context, and takes its name from the const keyword in those languages.

The idea of const-ness does not imply that the variable as it is stored in the computer's memory is unwriteable. Rather, const-ness is a compile-time construct that indicates what a programmer may do, not necessarily what they can do.

In addition, a method can be declared as const. In this case, the this pointer inside such a method is of const ThisClass* const type rather than of ThisClass* const type. This means that non-const methods for this object cannot be called from inside such a method, nor can member variables be modified. In C++, a member variable can be declared as mutable, indicating that this restriction does not apply to it. In some cases, this can be useful, for example with caching, reference counting, and data synchronization. In these cases, the logical meaning (state) of the object is unchanged, but the object is not physically constant since its bitwise representation may change.

The C++11 standard adds additional notes on the meaning of const when used in the Standard Library with respect to thread access and possible modifications of those const objects.

More about const-correctness here.

10286 questions
538
votes
9 answers

C# naming convention for constants?

private const int THE_ANSWER = 42; or private const int theAnswer = 42; Personally I think with modern IDEs we should go with camelCase as ALL_CAPS looks strange. What do you think?
mmiika
  • 9,970
  • 5
  • 28
  • 34
524
votes
11 answers

Static constant string (class member)

I'd like to have a private static constant for a class (in this case a shape-factory). I'd like to have something of the sort. class A { private: static const string RECTANGLE = "rectangle"; } Unfortunately I get all sorts of error from…
lb.
  • 5,666
  • 3
  • 17
  • 16
523
votes
31 answers

Use of 'const' for function parameters

How far do you go with const? Do you just make functions const when necessary or do you go the whole hog and use it everywhere? For example, imagine a simple mutator that takes a single boolean parameter: void SetValue(const bool b) { my_val_ = b;…
Rob
  • 76,700
  • 56
  • 158
  • 197
478
votes
17 answers

What is the difference between the "const" and "final" keywords in Dart?

What is the difference between the const and final keywords in Dart?
Ishmal Ijaz
  • 5,317
  • 3
  • 11
  • 17
458
votes
6 answers

const vs constexpr on variables

Is there a difference between the following definitions? const double PI = 3.141592653589793; constexpr double PI = 3.141592653589793; If not, which style is preferred in C++11?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
454
votes
9 answers

What is the difference between "const" and "val"?

I have recently read about the const keyword, and I'm so confused! I can't find any difference between const and the val keyword, I mean we can use both of them to make an immutable variable, is there anything else that I'm missing?
Mathew Hany
  • 13,310
  • 4
  • 19
  • 16
450
votes
17 answers

PHP Constants Containing Arrays?

This failed: define('DEFAULT_ROLES', array('guy', 'development team')); Apparently, constants can't hold arrays. What is the best way to get around this? define('DEFAULT_ROLES', 'guy|development team'); //... $default = explode('|',…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
428
votes
6 answers

Why can't I have "public static const string S = "stuff"; in my Class?

When trying to compile my class I get an error: The constant 'NamespaceName.ClassName.CONST_NAME' cannot be marked static. at the line: public static const string CONST_NAME = "blah"; I could do this all of the time in Java. What am I doing…
jjnguy
  • 136,852
  • 53
  • 295
  • 323
404
votes
20 answers

Declaring static constants in ES6 classes?

I want to implement constants in a class, because that's where it makes sense to locate them in the code. So far, I have been implementing the following workaround with static methods: class MyClass { static constant1() { return 33; } static…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
391
votes
19 answers

Why does JSHint throw a warning if I am using const?

This is the error I get when using const: My code looks like this: const…
Andre Schlesinger
  • 3,925
  • 2
  • 12
  • 6
388
votes
18 answers

Const in JavaScript: when to use it and is it necessary?

I've recently come across the const keyword in JavaScript. From what I can tell, it is used to create immutable variables, and I've tested to ensure that it cannot be redefined (in Node.js): const x = 'const'; const x = 'not-const'; // Will give an…
axdg
  • 4,025
  • 3
  • 13
  • 7
370
votes
28 answers

What is the best way to implement constants in Java?

I've seen examples like this: public class MaxSeconds { public static final int MAX_SECONDS = 25; } and supposed that I could have a Constants class to wrap constants in, declaring them static final. I know practically no Java at all and am…
mk.
  • 26,076
  • 13
  • 38
  • 41
367
votes
3 answers

"sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers" warning

I have Constants NSString, that I want to call like: [newString isEqualToString:CONSTANT_STRING]; Any wrong code here? I got this warning: sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers How should these be…
user4951
  • 32,206
  • 53
  • 172
  • 282
365
votes
19 answers

What is the difference between char * const and const char *?

What's the difference between: char * const and const char *
LB.
  • 13,730
  • 24
  • 67
  • 102
364
votes
15 answers

Constants in Kotlin -- what's a recommended way to create them?

How is it recommended to create constants in Kotlin? And what's the naming convention? I've not found that in the documentation. companion object { //1 val MY_CONST = "something" //2 const val MY_CONST = "something" //3 val…
Jodimoro
  • 4,355
  • 3
  • 11
  • 18