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
.
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
.
Create an instance of FoodMenu
and use it to call printMenu()
FoodMenu foodMenu = new FoodMenu();
foodMenu.printMenu();
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.
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();