Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: compiler error regarding method call

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default compiler error regarding method call

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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
    printx(){
    print(x);
    }
    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: compiler error regarding method call

    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.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default 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.

    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.

  8. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: compiler error regarding method call

     
    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

     
    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
        }
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default 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.

  10. The Following User Says Thank You to Norm For This Useful Post:

    dan1967 (December 18th, 2011)

  11. #10
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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
     
    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

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: compiler error regarding method call

    Having so many methods with the same name looks like a disaster waiting to happen.

Similar Threads

  1. Replies: 2
    Last Post: October 30th, 2011, 06:37 PM
  2. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  3. Map compiler error
    By kc120us in forum Collections and Generics
    Replies: 4
    Last Post: September 21st, 2011, 10:53 PM
  4. Java 6.0 Compiler Error
    By jilomes in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2011, 04:34 PM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM