Here is the following code:
class Sample{
private void display(Sample this, int x){
System.out.println(x);
}
public static void main(String[] args) {
Sample s = new Sample();
s.display(10);
}
}
The above code is working fine if I do not pass reference variable of Sample class as an actual argument i.e. s.display(10)
.
I am aware of the fact that from JDK 8
onwards we are allowed to pass this as parameter(formal arguments) and that too as the first argument and compiler internally inserts Sample this
as first argument as parameter(or formal argument) i.e. private void display(Sample this.....
if I do not write Sample this
as pararemeter (formal argument) explicitly.
But if I try to pass reference variable as argument(actual argument) i.e. s.display(s, 10)
, then the code doesn't compile even when both the actual and formal arguments are same.
eg:
class Sample{
private void display(Sample this, int x){
System.out.println(x);
}
public static void main(String[] args) {
Sample s = new Sample();
s.display(s, 10);
}
}
What's the reason behind of not compiling the above code?