Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Variable declaration and scope


Understanding how variable declaration is done and its scope is very important.

Declaring a variable

You can declare a variable by using the var keyword followed by the name of the variable. There are two syntaxes to declare a variable: one is to declare variables at class level, and another one is to declare local variables in blocks of instructions (inside functions, for example).

At class level

The following is the syntax you can use to declare a variable at the class level:

[public|private] [static] var varName [: varType] [= someValue];

Note that if you don't specify public or private, all members will be private unless the class implements the Public interface.

Static variables are variables that will be stored directly inside the class and not inside the instance of the class (this does mean that there will be only one value for them in the whole program). A static variable can only be accessed through the class, not through its instances. By the way, unlike...