Hello everybody !!!
Given the code below:
package JavaLibrary1; import java.io.*; public class Print { public Print(){ System.out.println("HELLO");}; public static void print(Object obj) {System.out.println(obj);} public static void print() {System.out.println(); } public static void printnb(Object obj) { System.out.print(obj); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// package chapter8ex3; import static JavaLibrary1.Print.*; class Foo { int x=9; // void print(int x){} (1) if a compile with this method its OK void print(){ print(x); //(2) gives complier error //JavaLibrary1.Print.print(x); (3) if I’m explicit its OK } } public class Chapter8Ex3 { public static void main(String[] args) { Foo d=new Foo(); d.print(); print("HH"); // (4) it compiles } }
I have the following Q:
When I compile the code I get an error at line marked (2) with a message saying :
„print() in chapter8ex3.Derived cannot be applied to (int) print(x); 1 error”
The same call from method main() marked at Line (4) or at line marked (2) its OK
However if I’m explicit as in line (3), it compiles.
If I change the name of the method print() from class Foo to printx() for example, the code compiles
I think it has something to do with the fact that print() and print(x) have the same name, even though they don’t have the same args.
I looked up in JLS 7.0 and I found this under Shadowing 6.4.1:
”A declaration d of a method named n shadows the declarations of any other methods
named n that are in an enclosing scope at the point where d occurs throughout the
scope of d.”
But if this is true then line (1) should give an error but it doesn’t !!!!
My question is: where in JLS or elsewhere this problem is dealt with ?
Thanks