Questions tagged [class-constants]

In object-oriented programming, a class constant is a constant defined within a class.

In object-oriented programming, a class constant is a defined within a .

97 questions
654
votes
10 answers

How to implement class constants?

In TypeScript, the const keyword cannot be used to declare class properties. Doing so causes the compiler to an error with "A class member cannot have the 'const' keyword." I find myself in need to clearly indicate in code that a property should not…
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
161
votes
12 answers

Can I get CONST's defined on a PHP class?

I have several CONST's defined on some classes, and want to get a list of them. For example: class Profile { const LABEL_FIRST_NAME = "First Name"; const LABEL_LAST_NAME = "Last Name"; const LABEL_COMPANY_NAME = "Company"; } Is there…
Brock Boland
  • 15,870
  • 11
  • 35
  • 36
139
votes
8 answers

Get value of dynamically chosen class constant in PHP

I would like to be able to do something like this: class ThingIDs { const Something = 1; const AnotherThing = 2; } $thing = 'Something'; $id = ThingIDs::$thing; This doesn't work. Is there a straightforward way of doing something…
Ben
  • 68,572
  • 20
  • 126
  • 174
113
votes
11 answers

How do you define a class of constants in Java?

Suppose you need to define a class which all it does is hold constants. public static final String SOME_CONST = "SOME_VALUE"; What is the preferred way of doing this? Interface Abstract Class Final Class Which one should I use and…
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
80
votes
5 answers

What is the purpose of the Java Constant Pool?

I am currently trying to dig deeper into the specification of the Java Virtual Machine. I have been reading Inside the JVM book online and there is one confusing abstraction I can't seem to grasp: Constant Pool. here is the excerpt from the…
Bober02
  • 15,034
  • 31
  • 92
  • 178
61
votes
6 answers

Is it possible to define a class constant inside an Enum?

Python 3.4 introduces a new module enum, which adds an enumerated type to the language. The documentation for enum.Enum provides an example to demonstrate how it can be extended: >>> class Planet(Enum): ... MERCURY = (3.303e+23, 2.4397e6) ... …
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
54
votes
5 answers

How to I make private class constants in Ruby

In Ruby how does one create a private class constant? (i.e one that is visible inside the class but not outside) class Person SECRET='xxx' # How to make class private?? def show_secret puts "Secret: #{SECRET}" …
DMisener
  • 891
  • 1
  • 8
  • 15
28
votes
7 answers

php class constant visibility

Can we set visibility of class constant? For this example: class MyClass { const CONST_VALUE = 'A constant value'; } Can we specify public const CONST_VALUE = 'A constant value'; or private const CONST_VALUE = 'A constant value'; or…
Poonam Bhatt
  • 10,154
  • 16
  • 53
  • 72
23
votes
1 answer

Can traits have properties & methods with private & protected visibility? Can traits have constructor, destructor & class-constants?

I've never seen a single trait where properties and methods are private or protected. Every time I worked with traits I observed that all the properties and methods declared into any trait are always public only. Can traits have properties and…
user9059272
14
votes
1 answer

Can I access a PHP Class Constant using a variable for the constant name?

When accessing a class constant I see that I can use a variable for the class name, e.g. $classname::CONST_VALUE. What if I want to use a variable for the constant name, e.g. self::$constant. This does not seem to work. Is there a workaround?
DatsunBing
  • 8,684
  • 17
  • 87
  • 172
8
votes
1 answer

Accessing Class Constants from a Class Reference variable in Delphi

I'm using Delphi 2007 to maintain an old project, I have a problem accessing class constants from a Class Reference variable, I get always the parent class constant instead of the children one. Suppose to have a parent class, some child classes, a…
MtwStark
  • 3,866
  • 1
  • 18
  • 32
8
votes
7 answers

Why constant data member of a class need to be initialized at the constructor?

I want to know why constant data member of a class need to be initialized at the constructor and why not somewhere else? What is the impact of doing so and not doing so? I also see that only static constant integral data can be initialized inside…
Abhineet
  • 6,459
  • 10
  • 35
  • 53
6
votes
2 answers

Combining bit flags in a class constant PHP

Possible Duplicate: Workaround for basic syntax not being parsed I am trying to allow developers to specify any combination of bits to specify which pieces of data they want included in a response. class ClassName { const…
5
votes
4 answers

Constant class members, assignment operator and QList

Please conform if I am correct and tell me whether there is a better solution: I understand that objects with constant members like int const width; can not be handled by the synthetic assignment operator that is implicitly created by the compiler.…
problemofficer
  • 435
  • 4
  • 15
5
votes
2 answers

Correct way to check if a class has a constant defined with PHPUnit

I am trying to find out the best, or correct, way to check if a class has a constant defined with PHPUnit. The PHPUnit docs don't seem to cover this, which makes me wonder if I am doing the correct thing by testing this - however it is an important…
crmpicco
  • 16,605
  • 26
  • 134
  • 210
1
2 3 4 5 6 7