1 2 Previous Next 22 Replies Latest reply: Jun 10, 2014 10:42 PM by Nhuy Van Go to original post RSS
      • 15. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
        rp0428

        That doesn't show the problem you posted at all. This is what you first said:

        For some reason, every time I use the "default" keyword in an interface, it gives me an error similar to "Syntax error on token default",  I deleted the "default" keyword, the error is gone.

        This is what you post now:

        This is my simple interface:                                                                           

        public interface Actions{

             void eat();

             default void run() {

             System.out.println(AnimalName + " can run 10 miles without stopping!!!");

            }

        }

        . . .

        C:\Users\NhuY>javac Actions.java

        Actions.java:4: error: cannot find symbol

             System.out.println(AnimalName + " can run 10 miles without stopping!!!");

                                ^

            symbol:   variable AnimalName

            location: interface Actions

        1 error

        See that error? It says: 'cannot find symbol'  Then it tells you what symbol it can't find: 'symbol: variable AnimalName'.

         

        The error is NOT 'Syntax error token default'. And the error would NOT be gone if you delete the "default" keyword.

         

        There IS NO variable called 'AnimalName' in that interface - that is the problem: not the word 'default'.

         

        You can't refer to objects that don't exist.

         

        • 16. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
          theRealPedron

          can you post a screenshot of the "installed JREs" window in Eclipse? I'm telling you that's the problem. If you've got 1.8 installed, available, and checked, then you should be able to get any functionality out of it that exists.

           

          Are you sure you're using the new 'default' keyword properly? Another thing to consider.

           

          I THINK you replied to my message about checking the installed JRE, but will you just confirm directly that you've check this in Eclipse?

           

          Throw us a bone here!

          • 17. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
            Nhuy Van

            Sorry, for some reason, I am not allowed to insert images, so I have to pose the screenshots this way, apologies.

            This image show errors on the "default" keyword and the installed JRE:

            https://www.dropbox.com/s/qhjl93imzryapt7/Errors1.png


            This is what I got after I ran the class Animal.java:

            https://www.dropbox.com/s/sh3twz9af1qynbz/Errors2.png

            • 18. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
              Nhuy Van

              It happened because I used the command-line tool rather than running it using Eclipse. The two image links above show the errors in Eclipse. Man, this is so frustrating, I just hate errors and my stupid self.

              • 19. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                rp0428

                  It happened because I used the command-line tool rather than running it using Eclipse. The two image links above show the errors in Eclipse.

                No - it happened for the reasons I said:

                There IS NO variable called 'AnimalName' in that interface - that is the problem: not the word 'default'.

                 

                You can't refer to objects that don't exist.

                That interface code won't compile from the command line or from within an IDE because it has ERRORS.

                public interface Actions{

                     void eat();

                     default void run() {

                     System.out.println(AnimalName + " can run 10 miles without stopping!!!");

                    }

                }

                Where do you expect Java to get the value of 'AnimalName' from? It doesn't exist anywhere. You define it in another class like this:

                public String AnimalName;

                That means it is an 'instance' variable. You can't refer to instance variables except in the context of an 'instance'. The interface has NO instance of Animal.

                • 20. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                  Nhuy Van

                  Ah hah, now it worked , compiled and ran with no problem, it parsed the default method and lamda expression smoothly, of course, after your suggestion about the instance variable reference mistake I made.

                  C:\Users\NhuY>java myAnimal

                  What kind of Animal?...Human

                  It is Human

                  Human loves ice cream!

                  What kind of Animal?...human

                  It is human

                  human can run 10 miles without stopping!!!

                   

                  But somehow, Eclipse won't run my simple program still (if the images won't load when you click on the links, try right-clicking and selecting "open link in new tab".)

                  https://www.dropbox.com/s/qhjl93imzryapt7/Errors1.png (Installed JREs)

                   

                  https://www.dropbox.com/s/sh3twz9af1qynbz/Errors2.png (Eclipse won't compile,  it display the squiggly lines on lamda expression)

                   

                  https://www.dropbox.com/s/0goj20axsdu8rhy/Errors3.png (I have corrected the instance variable reference in my simple interface as you suggested, but as you can see, the "default" keyword gets underlined with a red "x" indicator)

                   

                  I suspect that I haven't configured Eclipse correctly, the command-line tool compiles and runs my simple program with ease, but Eclipse refuses to do it, but I don't know what it is wrong with Eclipse's configuration though.

                  Thanks a ton buddy.

                  • 21. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                    rp0428

                    Eclipse questions need to be posted in an Eclipse forum.

                     

                    The first thing to do to troubleshoot a problem is to REMOVE everything that is NOT necessary. You want the simplest possible set of code and objects that demonstrate the problem.

                     

                    For Java (and your use case) that means eliminating any GUI or other third-party software.

                     

                    Please mark the thread ANSWERED if appropriate.

                    • 22. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                      Nhuy Van

                      Ok, gotcha, at least, I know it is not me type the wrong codes. Thanks a ton buddy, Where can find "Eclipse" discussion, I searched but couldn't find it,  it would be greatly appreciated if you could send me the link to it. Thank you very much rp0428

                      1 2 Previous Next