Forum Stats

  • 3,875,465 Users
  • 2,266,926 Discussions
  • 7,912,225 Comments

Discussions

Eclipse does not recognize "Default Method keyword" and "Lamda expression"

Nhuy Van
Nhuy Van Member Posts: 13
edited Jun 10, 2014 11:37PM in New To Java

Hi guys, I just started learning Java, so I don't know much about it. I have this very annoying issue with Eclipse (I have the latest version installed). 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. The same thing happens with "Lamda expression as well", say I have this object like this Actions myActions = () -> {System.out.print("Blah blah blah");};   , Eclipse also displays the error message similar to "Method body expected after (), delete '->' ". I checked the Java version I have, it is the latest one also, I don't know what it's wrong here, any suggestions are greatly appreciated. Thank you very much for your time and help.

Best Answer

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

«13

Answers

  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy

    Don't just post a description and singular snippets of the code - post the actual code. And don't post a description of the error, post the error.

  • Java 1.5 does NOT support the DEFAULT keyword or lamda expressions.

    I'm just guessing what Java version you are using since you didn't post:

    1. The Java version your code is using

    2. The Eclipse version

    3. The Java version Eclipse is using

    4. Your code

    5. The actual error and stacktrace you are getting

    Without that all anyone can do is 'guess'.


  • Nhuy Van
    Nhuy Van Member Posts: 13
    edited Jun 9, 2014 12:54AM

    SORRY, FOR SOME REASON, I AM NOT ALLOWED TO ATTACHE IMAGES, SO I HAD TO COPY AND PASTE EVERYTHING TO HERE

    ECLIPSE VERSION NUMBER I HAVE

    Version: Kepler Service Release 2

    Build id: 20140224-0627

    (c) Copyright Eclipse contributors and others 2000, 2014.  All rights reserved.

    Visit http://eclipse.org/

    ----------------------------------------------

    JAVA VERSION NUMBER I HAVE:

    Version 8 Updates 5  (build 1.8.0_05-b13)

    ------------------------------------------------------------------------------------------------------------------------------------

    THIS IS THE INTERFACE I AM MENTIONING

    public interface Actions{

         void eat();

         default void run() {

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

        }

    }

      /*

       --- BELOW IS THE ERROR DETAIL I GET WHEN I USE THE DEFAULT METHOD ---

              -Syntax error on token "default", delete this token

      */

      /*

       --- WHEN I CHANGE "DEFAULT" TO "STATIC", HERE IS WHAT I GET ---

                Multiple markers at this line

           - Abstract methods do not specify a body

           - Illegal modifier for the interface method Run; only public & abstract are permitted

      */

    -------------------------------------------------------------------------------------------------------------------------------------

    THIS IS THE SOURCE FILE WITH THE LAMDA EXPRESSION

    import java.util.Scanner;

    public class Animal {

          static Scanner myScanner = new Scanner(System.in);

          public String AnimalName;

         {

             System.out.print("What kind of Animal?...");

            AnimalName = myScanner.next();

         }

         public Animal() {

               System.out.println("It is " + AnimalName);

          }

    }

    class myAnimal  {

            public static void main(String[] args) {

            Animal Human =  new Animal();

          Actions myHuman = () -> {System.out.println(Human.AnimalName + " loves ice cream!") ; } ;

          myHuman.eat();

          myHuman.run();

      /*

          --- THIS IS THE ERROR DETAIL ---

       Multiple markers at this line

           - Syntax error on tokens, delete these tokens

           - Syntax error on token(s), misplaced construct(s)

           - println cannot be resolved or is not a field

           - Syntax error on token "}", delete this token

      */

      }

    }

  • Those errors are telling you that you are NOT using Java 8 for the compilation.

    1. Open a command window
    2. type 'javac -version' and post the results

    3. if that shows version 8 compile your simple interface and post the results of the compilation.

  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy

    Good, text is way better than stupid screenshots from which you cannot copy/paste. Do realize that writing in all uppercase is considered shouting or taking an aggressive tone, there is no need to do that.

  • Nhuy Van
    Nhuy Van Member Posts: 13

    , you are way sensitive buddy, I used uppercase so that you can identify each section I mention easier, no yelling or shouting intended.

  • Nhuy Van
    Nhuy Van Member Posts: 13

    JAVA VERSION NUMBER I HAVE:

    Version 8 Updates 5  (build 1.8.0_05-b13)

    I have java 8, maybe because Eclipse just doesn't recognize lamda or default method?

  • I fail to see how ANY of what you posted is related to what I ask you to do.


  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy

    You make too many assumptions, I was only passing you a hint so you may prevent passing off the wrong idea to other people in the future. Because yes, some people are sensitive to such things, so don't do things that are outside of common forum etiquette.

    If you want to identify sections, the site has rich editing features that you can utilize. But I'd rather just use proper spacing and keep your posts to the point so you don't need to make the effort.

  • theRealPedron
    theRealPedron Member Posts: 4
    edited Jun 6, 2014 2:57PM

    So, just an FYI for using eclipse, the version you are using, and that comes through on your command-line "java -version" may not be the version you're using in Eclipse.

    First, you need to check what version of Java you're using in Eclipse:

    1. Open Eclipse
    2. Browse to the "Preferences" window
    3. Click on "Java"
    4. Click on "Installed JREs"
    5. In this window you will see a list of directories. Each should point to the JAVE_HOME directory of a separate installation. Do you see the checked one pointing to a Java 8 installation? If NO, then do the following steps:
      1. Click 'Add'
      2. Click on 'Standard VM' and 'Next'
      3. Under the 'JRE_HOME' field, click on 'Directory'
      4. Browse to the JAVA_HOME directory (this is the base directory, where your 'bin', 'include', and 'lib' directories reside) for your Java 8 installation
      5. Once you click it, if it's a valid JAVA_HOME directory, all the info should fill in automatically.
      6. Click Finish
      7. Once you finish this, make sure this entry is checked. Click OK.
      8. Go back and try again. GOOD LUCK!
This discussion has been closed.