Results 1 to 5 of 5
  1. #1
    Andyroxxx is offline Member
    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Default Include a call in the main method

    Hi,

    I have created a program call 'Fruits' and have added another class call 'FruitOrder'.
    How do I call a method by including a call in the main method?

    Code for FruitOrder is:

    public class FruitOrder {
    private char fruitOrder;
    private double weight;
    private String message;

    FruitOrder (char fruitCode, double weight) {
    this.fruitCode = fruitCode;
    this.weight = weight;
    }
    public String calculate() {
    double price = 0;
    String fruitType = "";
    boolean isValidCode = true;

    switch (friutCode) {
    case 'A':
    fruitType ="Apple";
    price = weight * 1.75;
    break;
    case 'B':
    fruitType ="Banana";
    price = weight * 2.65;
    break;
    default:
    isValidCode = false;
    message = "iInvalid fruit cade entered";
    break;
    }
    if (isValidCode) {
    message = "You are buying " + weight + "Kgs of " + fruitType + " for $" + price;
    }
    return message;
    }
    }


    This code is entered in the 'Fruits' class:

    public static void case1() {
    FruitOrder fo = new FruitOrder ('A', .5);
    System.out.println ("case 1: " + "\n" + fo.calculate());
    }

    This is my question? how do I call this method by including a call in the main method as follow:
    case1();

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default Re: Include a call in the main method

    how do I call this method
    To call a static method in a class use the classname dot the method name: ClassName.methodName();
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Include a call in the main method

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Include a call in the main method

    Quote Originally Posted by Andyroxxx View Post

    This is my question? how do I call this method by including a call in the main method as follow:
    case1();
    Try this:
    you call this case1() method into main method like below...

    Java Code:
    Fruits fr=new Fruits();
    fr.case1();
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: Include a call in the main method

    It's a static method.
    You don't need (and shouldn't use) an instance of Fruit to call it.
    Java Code:
    Fruit.case1();
    is all you should use.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Call to main() twice ??
    By Bgreen7887 in forum New To Java
    Replies: 13
    Last Post: 07-19-2012, 04:41 AM
  2. Changing a code to include a main method
    By Shlr in forum New To Java
    Replies: 3
    Last Post: 04-23-2012, 04:33 PM
  3. how can main call other methods?
    By rahuld.exe in forum New To Java
    Replies: 1
    Last Post: 04-06-2012, 01:19 PM
  4. passing hashmap from a method and call it within main
    By rajuchacha007 in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 09:41 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:16 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
  •