Reputation: 1363
We have self registering subclasses of 'Handler' which we want to access through Subclass.me(). Is something similar to this possible in Java: ?
public class Handler{
static Vector<Handler> register=new Vector<Handler>();
public static Handler me() {
return register.get( this.class);// TODO
}
}
public class SubClass extends Handler{
SubClass(){register.add(this);}// OK
}
To clarify the question: Is it possible to retrieve the CLASS when calling a static java method? this.class obviously doesn't work, because 'this' is not available.
Upvotes: 0
Views: 203
Reputation: 9134
In java, you cannot make a static reference to the non-static method/variable. So,
Because, the static method and variable are belong to the Class not to the Instance while the non-static method and variable are belong to the Instance not to the Class.
Upvotes: 0
Reputation: 466
Static methods belong to the class. They cannot be overridden.
MyClass.myStaticMethod()
is the only correct way of accessing a static method.
Upvotes: 4