AOSMTO - Access Of Static Members Through Objects
Static members should be referenced through class names rather than through objects.
Wrong
void func () {
AOSMTO1 obj1 = new AOSMTO1();
AOSMTO2 obj2 = new AOSMTO2();
obj1.attr = 10;
obj2.attr = 20;
obj1.oper();
obj2.oper();
this.attr++;
this.oper();
}
static int attr;
static void oper () {}
}
class AOSMTO2 {
static int attr;
static void oper () {}
}
Tip: Always reference static members via class names
Right
void func () {
AOSMTO1 obj1 = new AOSMTO1();
AOSMTO2 obj2 = new AOSMTO2();
AOSMTO1.attr = 10;
AOSMTO2.attr = 20;
AOSMTO1.oper();
AOSMTO2.oper();
AOSMTO1.attr++;
AOSMTO1.oper();
}
static int attr;
static void oper () {}
}
class AOSMTO2 {
static int attr;
static void oper () {}
}