-
Notifications
You must be signed in to change notification settings - Fork 0
Variables
As mentioned in the previous section, there are two types of variables in Aussom, local and member variables. Local variables exist (in scope) within a given function. Once leaving the function, local variables are not accessible and may be cleaned up by the Java garbage collector. Member variables persist as long as the object persists, and can be accessed from member functions or from outside the object if they are public.
Here are examples of defining local variables, they are in bold below.
The two arguments are local variables. When the function is called, the values passed to the function will be stored in the local variable list by the names provided in the function definition.
The other 5 are examples of assigning new local variables. After a variable is assigned, it can be used with that function.
myFunct(ArgumentOne, ArgumentTwo) {
state = "California";
pcode = 95678;
elevation = 100.234;
warmClimate = true;
myObj = new someObject();
}
Here is an example of member variables. The major difference between local and member variables are that member variables have access modifiers and they persist for the lifetime of the object.
Member variables accessability are defined as either public or private. If you do not specify an access modifier, the member is assumed to be private. Public means that any other function can modify the variable and private means that only member functions can modify the variable.
In the example below, we define one public and one private member variable. Notice that we define a public member function that allows any other function to get the value of onlyMyMemberFuncts variable. In order to access a member variable or function, you need to use the 'this' key word. 'this' tells Aussom that you mean a member variable or function, and is literally a reference to the current object.
class myClass {
public everyoneCanSeeMe = "Hi";
private onlyMyMemberFuncts = true;
public getPrivateMember() {
return this.onlyMyMemberFuncts;
}
}