In Java, is it possible to determine whether a static method is called from either an instance of the object, or statically (SomeClass.method()
)?
To give you an a better idea of what I'm talking about, take look at this code below:
public class SomeClass {
public static void staticMethod() {
if (/*called from instance*/) {
System.out.println("Called from an instance.");
} else if (/*called statically*/){
System.out.println("Called statically.");
}
}
public static void main(String[] args) {
new SomeClass().staticMethod();//prints "Called from an instance."
SomeClass.staticMethod();//prints "Called statically."
}
}
I understand it isn't good practice to call a static method from an instance, but still, is it possible to differentiate between these two calls? I was thinking that the Reflection API probably holds the key to this.
Aucun commentaire:
Enregistrer un commentaire