In all the super
keyword tutorials I found online, it's hard to get any examples closer to following one.
My question:
What's the difference between Tracker.super.track(event);
and test.parent.Tracker.track(event);
?
Why would the first work?
What does Tracker.super
refers to? A object or a class?
subclass:
package test;
public class Tracker extends test.parent.Tracker {
@Override
public void track(final Event event) {
Executor.execute(new Runnable() {
public void run() {
Tracker.super.track(event); //this works!! why??
super.track(event); // compile error: cannot resolve
test.parent.Tracker.track(event); //compile error: can only reference static method
}
});
}
}
super class
package test.parent;
public abstract class Tracker {
public void track(Event event) {}
}
Reference Updates:
In jls8, 15.11.2
"Suppose that a field access expression T.super.f appears within class C, and the immediate superclass of the class denoted by T is a class whose fully qualified name is S. If f in S is accessible from C, then T.super.f is treated as if it had been the expression this.f in the body of class S. Otherwise, a compile-time error occurs.
Thus, T.super.f can access the field f that is accessible in class S, even if that field is hidden by a declaration of a field f in class T.
It is a compile-time error if the current class is not an inner class of class T or T itself."
Where the "super" keyword in Java is used as a reference to the object of the superclass. This implies that to use "super" the method should be invoked by an object, which static methods are not. Therefore, you cannot use the "super" keyword from a static method.
super is used to refer super-class's instance as well as static members. super is also used to invoke super-class's method or constructor.
Definition and Usage The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.
Yes, you can call the methods of the superclass from static methods of the subclass (using the object of subclass or the object of the superclass).
Your run()
method is in an anonymous subclass of Runnable
where it is also an inner class of Tracker
.
Effectively the same as
package test;
public class Tracker extends test.parent.Tracker {
...
@Override
public void track(final Event event) {
//anonymous class translated into local class
class TrackerRunnable implements Runnable {
public void run(){
Tracker.super.track(event); //this works!! why??
super.track(event); // compile error: cannot resolve
test.parent.Tracker.track(event); //compile error: can only reference static method
}
}
Executor.execute(new TrackerRunnable());
}
}
In Java an inner class also has a reference to the outer class that it "belongs" to. You would reference this
for TrackerRunnable
as this
inside of run, but if you need to access the instance of Tracker
that the TrackerRunnable
is associated with you would access it as Tracker.this
. Same thing with Tracker.super
. Just super
means the superclass of TrackerRunnable
not Tracker
(in this case Runnable
).
The main thing to note is that this is syntax that is used for scope resolution only in inner classes, and that the Tracker
here refers to "The instance of the Tracker
class that I belong to". In the case of test.parent.Tracker.track
Tracker
refers to "the Tracker
class", so you can't call instance members on the class itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With