Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does each object allocate memory for constants?

In my code design, I've included a lot of constants. When a new object is created, is memory allocated for that object's constants, or is it stored permanently in a single instance, like a static variable is? In terms of memory storage, where exactly do static items end up?

In other words, if I define 100 objects, will there be 100 copies of the same constant value?

If they are defined in static memory (wherever that is), and I would expect that they are, does accessing them require the computer to switch memory pages? Is there a performance hit for constantly accessing constants instead of variables?

Thanks

Ares

like image 532
Athena Avatar asked Jul 16 '26 22:07

Athena


1 Answers

Constants have no storage location at runtime. All access to constant identifiers results in the literal value of that constant replacing the identifier when the code is compiled.

like image 102
Servy Avatar answered Jul 19 '26 10:07

Servy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!