this and super are keywords aren’t they; then how can I use them for passing arguments to constructors the same way as with a method??
In short how is it that both can show such distinct behaviors??
this and super are keywords aren’t they; then how can I use them for
Share
You are correct that both
thisandsuperare keywords. The Java language specification defines explicitly how they must behave. The short answer is that these keywords behave specially because the specification says that they must.According to the specification
thiscan be used a primary expression (only in certain places) or in an explicit constructor invocation.So you can use
thisas an argument to a function to pass a reference to the current object. However note that you cannot usesuperin the same way as it is not a primary expression:Result:
You can use
super.foothough because this is defined in 15.11 to be valid:The specification also puts restrictions on how super can be used: