Mr A Mr A - 1 month ago 12
Java Question

How to Run private class method.in same class

Can figure why it won't allow me to run the

displaymainMethod
because it's private even though I know I can run it from from same class. Is there a way to do this without using reflective API?

This is the error

Error:(10, 9) java: cannot find symbol
symbol: class displayMainMenu
location: class LoginPrototype


Code

import java.util.*;

public class LoginPrototype {

public static void main(String[] args) {

ArrayList<Credentials> allUsers = new ArrayList<Credentials>();
displayMainMenu mainMenu = new displayMenu();
}

private void displayMainMenu() {
int input;
do {
System.out.println("Menu Options");
System.out.println("[1] Login");
System.out.println("[2] Register");
System.out.println("[0] Quit");//5 Displaying Main Menu Options
Scanner sc = new Scanner(System.in);
input = sc.nextInt();

if (input > 2) {
System.out.println("Please enter a value of [0] - [2]");
}
else if (input == 1){
System.out.println("Login");
}
else if (input == 2){
System.out.println("Register");
}
else if (input == 0){
System.out.println("Thank you. bye.");
}
}while(input >= 2);
}
}

Answer
 displayMainMenu mainMenu = new displayMenu();

This line does not make sense. You are treating the function like an object. You should just call the function itself: displayMainMenu();

But make the function public.

Comments