Forums → Programming Forum → dont get static const
- 2 Replies
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
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.
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.
You must be logged in to post a reply!
We strive to deliver the best gaming experiences... on the internet and on your mobile phone. Play thousands of free online games, get access to free mmorpg games, online rpg games, fun online flash games, and more.
We offer free flash games in many different genres: online shooting games, online puzzle games, online war games, free online car games, free online hidden object games and dozens more.
This is the best place on the web to play online games for free... play on Armor Games! No matter what game style you prefer, we've got it here.
Gaming websites, passionate bloggers, and quirky streamers are welcome to share or review our games. If you need game keys, art, trailers, screenshots or more check out our press kit website.
Game developer? Visit developers.armorgames.com to request AG developer status, find documentation on our APIs, and get access to our development environment. Let's make sweet, sweet games together.
Looking for a publishing partner that can help your app rocket to success? Contact the mobile team to learn more about how we can help!