compiler error regarding method call
Hello everybody !!!
Given the code below:
Code Java:
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
Re: compiler error regarding method call
Please copy and paste the full and exact text of the compiler's error message. Your edited version leaves out info.
Where is the print() method defined that takes an int value as an argument?
Why is line marked (1) commented out? The definition in that line is what the compiler is looking for.
Re: compiler error regarding method call
Hi Norm
first below I give you the full compiler error message:
Compiling 1 source file to C:\TIJ4\Problems\Chapter8\Chapter8Ex3\build\classe s
C:\TIJ4\Problems\Chapter8\Chapter8Ex3\src\chapter8 ex3\Chapter8Ex3.java:24: print() in chapter8ex3.Derived cannot be applied to (int)
print(x);
1 error
C:\TIJ4\Problems\Chapter8\Chapter8Ex3\nbproject\bu ild-impl.xml:610: The following error occurred while executing this line:
C:\TIJ4\Problems\Chapter8\Chapter8Ex3\nbproject\bu ild-impl.xml:245: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 14 seconds
//////////////////////
Now, the print(x) call is defined in package JavaLibrary1 in class Print method print(Object obj)
I included in my code the package JavaLibrary1
at (1) I inserted the same method definition for print(Object obj) when I tried to figure out the problem.
If I uncomment lien (1) the program compiles , and method print(Object obj) from class Foo is called.
If I comment the method from line (1) I get an error as above.
If I call the method from JavaLibrary1 explicitly as in line (3) the program compiles.
If I rename method print() as printx() with the initial call inside print(x) as below
the program compiles
So as I mentioned before I think there is a name conflict between method print() in class Foo() and method
print(Object obj) in package JavaLibray1. I found some expalantion in JLS as I posted initially but it was not satisfactory.
Re: compiler error regarding method call
Quote:
print() in chapter8ex3.Derived cannot be applied to (int) print(x);
The compiler wants to find a method defined with the same signature as the method is being called with. If there is no print(int x) defined, then the compiler will give an error.
If you code: print(123)
then there must be a method defined: print(int anInt)
Does the compiler give you any messages about a name conflict? Please post them.
If you are going to post code and error messages, you need to give all of the code and all of the error messages. Your post shows an error message without the code. And then talks about several different versions of the code that may or may not have errors.
What is your objective? What are you trying to code that is not working the way you want it to.
Show the full code, the error messages and ask your questions.
Re: compiler error regarding method call
I apologize Norm, I renamed my class Derived from my program to class Foo here, so the eror mesaage is the same with Derived replaced by Foo as below:
Compiling 1 source file to C:\TIJ4\Problems\Chapter8\Chapter8Ex3\build\classe s
C:\TIJ4\Problems\Chapter8\Chapter8Ex3\src\chapter8 ex3\Chapter8Ex3.java:24: print() in chapter8ex3.Foo cannot be applied to (int)
C:\TIJ4\Problems\Chapter8\Chapter8Ex3\src\chapter8 ex3\Chapter8Ex3.java:24: print() in chapter8ex3.Foo cannot be applied to (int)
print(x);
1 error
C:\TIJ4\Problems\Chapter8\Chapter8Ex3\nbproject\bu ild-impl.xml:610: The following error occurred while executing this line:
C:\TIJ4\Problems\Chapter8\Chapter8Ex3\nbproject\bu ild-impl.xml:245: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 11 seconds)
maybe that caused some confusion for you
Re: compiler error regarding method call
Hi again,
I'm just trying to learn about the Java rules regarding shadowing/ name colission.
I used this program as an example for this. My code in the first post is all the code there is.
The problem I have is :
Why even if I use :import static JavaLibrary1.Print.*
I get an error if I call the method print(x) from inside a method print() WITH THE SAME NAME, but when I use the fully specified name as in JavaLibrary1.Print.print(x) the program works.
I thought it should work in both situations.
Re: compiler error regarding method call
You did not post the code that caused the errors shown in post #5
Are you getting confused because the names are the same? The names AND the arguments must be the same.
Quote:
I get an error if I call the method print(x) from inside a method print() WITH THE SAME NAME, but when I use the fully specified name as in JavaLibrary1.Print.print(x) the program works.
I thought it should work in both situations.
Post the code for this.
Re: compiler error regarding method call
Code Java:
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(){
print(x); //(2) gives complier error
}
}
public class Chapter8Ex3 {
public static void main(String[] args) {
Foo d=new Foo();
d.print();
print("HH"); // (4) it compiles prints HH
}
}
AND
Code Java:
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(){
JavaLibrary1.Print.print(x); (3) if I’m explicit its OK prints 9
}
}
public class Chapter8Ex3 {
public static void main(String[] args) {
Foo d=new Foo();
d.print();
print("HH"); // (4) it compiles prints HH
}
}
Re: compiler error regarding method call
Sorry, I have no idea why the compiler is unhappy. I've only used the static imports once and had no problems.
I try to keep my code simple and readable. Compiler short cuts can some times be confusing.
Re: compiler error regarding method call
I'm happy you see the problem. Of course in real life nobody would program like that.
I'm trying to figure it out becuase I'm preparing for java Certification Exam
Now look at the piece of code below
Code Java:
class Foo {
int x=9;
void printx(){ //after I rename print() to printx()
print(x); //(2) now its OK after I rename print() to printx()
}
}
now the code compiles. So the the error is because of the name conflict between method print() and print(x)
The only place to find an explanation is the JLS, but there I could not find anything similar to this, is true I just skimmed thru it.
The closest thing I found is 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.”
Thanks for you time Norm
Re: compiler error regarding method call
Having so many methods with the same name looks like a disaster waiting to happen.