ForumsProgramming Forumdont get static const

2 1428
88 posts

I just wanted to know if anyone here would be willing enough to explain to me what a static const is and what they do

  • 2 Replies
27 posts

Things that are static can be used by other classes without having to create an object of the class. For example, if you have a non-static constant in one class, you can't access that variable until you make an object of that class. If the variable were to be made static, I would not have to make a new object of a class to access the constants.

Lets say that I have a "Keys" class that I can use to get the keyCodes for KeyboardEvent listeners. Inside the class I have declared these constants:

const LEFT:int = 37
const UP:int = 38
const RIGHT:int = 39
const DOWN:int = 40

Since they're not static I would have to do something like this in whatever other class I'm using:
var keyNumbers:Keys = new Keys()
And then I could access them by going:
keyNumbers.LEFT or keyNumbers.RIGHT etc. to get the values.

But if I were make the constants static like so:

static const LEFT:int = 37
static const UP:int = 38
static const RIGHT:int = 39
static const DOWN:int = 40

In any other class, I can now simply go:
Keys.LEFT or Keys.RIGHT etc. to get the values
without ever having to create a new object of the keys class, like when I made the keyNumbers object.

569 posts

Pretty much what BlueJayy said... static = able to access this variable without instantiating, const = can be set only once, and can't be changed, usually used for variables that remain the same forever, ie. keyboard keys.

Showing 1-2 of 2