Results 1 to 4 of 4
  1. #1
    MylesPollie is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Program that implements a final method and performs and arithmetic operation

    class A
    {
    public final int Add(int a,int b)
    {
    return a+b;
    }
    }

    class B
    {
    public static void main (String args[])
    {
    System.out.println(Model.Add(5,10));
    }
    }

    This is what I have. I'm not sure if this even makes use of a final method. But when I run it I get an error saying cannot find symbol for line 13. Thanks for the help.

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

    Default Re: Program that implements a final method and performs and arithmetic operation

    There are a few ugly errors in your code:

    1) method names start with a lowercase letter;
    2) the identifier 'Model' isn't defined anywhere;
    3) what's the reason for making that method a final method?
    4) class B should be public by convention.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    MylesPollie is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Program that implements a final method and performs and arithmetic operation

    My assignment calls for it to use a final method. I edited my code but I'm getting "error: non-static method add(int,int) cannot be referenced from a static context

    class Model
    {
    public final int add(int a,int b)
    {
    return a+b;
    }
    }

    class B
    {
    public static void main (String args[])
    {
    System.out.println(Model.add(5,10));
    }
    }

    Thanks for your help

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: Program that implements a final method and performs and arithmetic operation

    The compiler is right again (of course), so you either make your method a public static final one, or you have to instantiate an object first and call your method on that object.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replies: 6
    Last Post: 09-03-2012, 06:31 PM
  2. Java Arithmetic Program. PLEASE HELP!
    By Ahdood in forum New To Java
    Replies: 9
    Last Post: 10-31-2011, 10:15 AM
  3. Replies: 1
    Last Post: 04-05-2010, 11:13 PM
  4. Replies: 24
    Last Post: 12-03-2009, 11:58 PM

Posting Permissions

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