Results 1 to 2 of 2
  1. #1
    Java Learner is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default identifier expected error

    Hi,

    I am trying out some code to understand abstract class and package, but hitting an error. Here are the details:

    I have the following classes:

    Fruit.java
    ---------

    package food;

    public abstract class Fruit{

    public abstract void eat();
    }

    Apple.java
    ----------

    package food;

    import food.Fruit;

    public class Apple extends Fruit{

    public void eat() {
    System.out.println("I am eating an Apple");
    }
    }

    Snack.java
    -----------
    package food;

    public class Snack{

    Apple myFruit = new Apple();
    myFruit.eat();

    }

    I am compiling using the following command: javac -d /java/code/classes <filename>.java

    I have added /java/code/classes to my CLASSPATH.

    I have compiled Fruit.java and Apple.java successfully. But when I try to compile Snack.java, I get an <identifier expected> error on line 6.

    What am I doing wrong? Thanks in advance for your help.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Java Learner View Post
    public class Snack{

    Apple myFruit = new Apple();
    myFruit.eat();

    }

    I have compiled Fruit.java and Apple.java successfully. But when I try to compile Snack.java, I get an <identifier expected> error on line 6.

    What am I doing wrong? Thanks in advance for your help.
    You can't put statements such as myFruit.eat() at the class level; you have to put them in the body of a method of that class. The Java syntax wants it that way and there's nothing we can do about it.

    kind regards,

    Jos

Similar Threads

  1. Error:identifier expected(Help!)
    By chhoton in forum New To Java
    Replies: 8
    Last Post: 09-22-2009, 05:42 PM
  2. Replies: 3
    Last Post: 12-05-2008, 09:34 PM
  3. getting identifier expected error . help me !
    By victorkeath in forum New To Java
    Replies: 3
    Last Post: 11-07-2008, 06:49 PM
  4. Identifier expected error
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 06:49 PM
  5. Error: <identifier> expected
    By barney in forum AWT / Swing
    Replies: 2
    Last Post: 07-31-2007, 08:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •