A non-static method cannot refer to a static context using methods and variables

When writing BookStoreApplication, using the Book, Tape, and CD classes to create objects. Although it is not complete, the application class should create new BookStoreItems, which are Book, Tape and CD. They inherit from the BookStoreItems class. In this application class, I get an error all the time:

error: non-static method printMenu() cannot be referenced from a static context
error: non-static method getUserChoice() cannot be referenced from a static context
error: non-static variable input cannot be referenced from a static context

I changed it as static and then not static, but I keep getting this error ...

import java.util.Scanner;

public class BookStoreApp2 {

    //constants for options
    static final int ADD_BOOK = 0;
    static final int ADD_TAPE = 1;
    static final int ADD_CD = 2;
    static final int QUIT = -1;

    Scanner input = new Scanner (System.in);

    public static void main(String[] args) {


        BookStoreItem[] item;//declaring array

        item = new BookStoreItem[10];//initializing array

        int itemType = -1;

        printMenu();
        getUserChoice();

        for (int i = 0; i < item.length; i++){
            System.out.print("\n" + i + "\tEnter 0 for Book, 1 for Tape, 2 for CD: ");
            itemType = input.nextInt();

            switch (itemType) {
                case 0:
                    item[i] = new Book();
                    break;
                case 1:
                    item[i] = new Tape();
                    break;
                case 2:
                    item[i] = new CD();
                    break;
                default: 
                    System.out.println("\nInvalid choice.");
            }//end of switch statement

        }//end of for loop

        for (int i = 0; i < item.length; i++) {
            System.out.println("\nAnimal #" + i + ": ");

            System.out.println("\n\tTitle: " + item[i].getTitle()); //polymorphic because they can operate on separate objects
            System.out.println("\n\tAuthor: " + item[i].getAuthor());
        }//end of for


    }//end of main method


//PRINT MENU----------------------------------------------------------  
    public void printMenu(){
        System.out.println("\nPress:");
        System.out.println("\t" + ADD_BOOK + "\tTo add a book to the book store.\n");
        System.out.println("\t" + ADD_TAPE + "\tTo add a tape to the book store.\n");
        System.out.println("\t" + ADD_CD + "\tTo add a CD to the book store.\n");
        System.out.println("\t" + QUIT + "\tTo exit\n");
    }
//---------------------------------------------------------------------
//GET USER CHOICE------------------------------------------------------ 
     public int getUserChoice() {
        int choice;     
        System.out.print("Please enter your choice: ");
        choice = input.nextInt();

        return choice;
    }//end of getUserChoice
//----------------------------------------------------------------------

}//end class
+5
source share
4 answers

- printMenu() getUserChoice() static, static main, , . non-static , .

:

BookStoreApp2 bookStoreApp = new BookStoreApp2();
bookStoreApp.printMenu();
bookStoreApp.getUserChoice();
+14

, , main() :

public BookStoreApp2()
{
   // Put contents of main method here
}

() . :

public void main( String[] args )
{
  new BookStoreApp2();
}
+4

1) printMenu(), getUserchoice() static

2) If you want to construct it better, move the logic from yours mainto a separate instance method. And then from maincreate a new instance of your class and call your instance method

+2
source

You should put Scanner input = new Scanner (System.in);in the main method, and not create an input object from the outside.

+1
source

All Articles