0

I want to print a method that is in another class. How do I do that?

Thanks!

The method name I am trying to call is public void printMenu()... within a class called FoodMenu.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Jen Fisher
  • 1
  • 1
  • 1
  • 1
  • You can also use friend function. It can be helpful => http://stackoverflow.com/questions/14226228/implementation-of-friend-concept-in-java – Md. Fahim Shahrier Rasel Mar 01 '16 at 19:00
  • welcome to stackoverflow, please take some time to read the how to ask a good question guide. I will help you get better answers in te future. :) http://stackoverflow.com/help/how-to-ask – toskv Mar 01 '16 at 21:44

3 Answers3

3

Create an instance of FoodMenu and use it to call printMenu()

FoodMenu foodMenu = new FoodMenu();
foodMenu.printMenu();
Guy
  • 46,488
  • 10
  • 44
  • 88
  • Thanks! I did this and an error message came "can't find class symbol" – Jen Fisher Mar 01 '16 at 19:02
  • @JenFisher There might be problem with the compilation. Look [here](http://stackoverflow.com/a/7373255/5168011) and [here](http://stackoverflow.com/a/5998081/5168011) – Guy Mar 01 '16 at 19:08
0

You will need an object of the class FoodMenu then you can call the method

Do someting like:

FoodMenu myFoodMenu = new FoodMenu(); // declaring the object
myFoodMenu .printMenu(); // calling the method.

if your class FoodMenu is in another package, then import it in the class you are going to define the object.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0
    public class FoodMenu{
    public void printMenu() { 
    System.out.println("here is menu sir!"); 

}
     public static void main(String[] arg) {
    FoodMenu f = new FoodMenu();
    f.printMenu();
    }
    }

FoodMenu object created by new keyword And with help of object you can call its method f.printMenu();

Mohsin AR
  • 2,998
  • 2
  • 24
  • 36