I even checked Java's use of the static keyword... and they provide a clear way to access a static variable from inside the class. By using the variable itself.
Code: Select all
// A java program to demonstrate use of
// static keyword with methods and variables
// Student class
class Student
{
// static variable
static String cllgName;
// instance variable
String name;
public Student(String name){
this.name = name;
}
// instance method
void getStudentInfo(){
// accessing instance variable
System.out.println("name : " + this.name);
// accessing static variable
System.out.println("cllgName : " + cllgName);
}
}
//Driver class
public class StaticDemo
{
public static void main(String[] args)
{
// setting static variable without instantiating Student class
Student.cllgName = "XYZ";
Student s1 = new Student("Alice");
Student s2 = new Student("Bob");
s1.getStudentInfo();
s2.getStudentInfo();
}
}
Keep in mind the following line:
Code: Select all
// accessing static variable
System.out.println("cllgName : " + cllgName);
Here's some AHK v2 code to accomplish a similar goal of accessing a static variable from within the class:
Code: Select all
a := new set
b := new set
c := new set
MsgBox Set.instances
a := b := c := ""
MsgBox Set.instances
class Set {
static instances := 0
__New() {
%this.__class%.instances++
}
__Delete() {
%this.__class%.instances--
}
}