不能从具有方法和变量的静态上下文中引用非静态方法 [英] Non-Static method cannot be referenced from a static context with methods and variables

查看:21
本文介绍了不能从具有方法和变量的静态上下文中引用非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写使用 Book、Tape 和 CD 类来创建对象的 BookStoreApplication.尽管未完成,但应用程序类应该创建新的 BookStoreItem,它们是 Book、Tape 和 CD.它们继承自 BookStoreItems 类.在这个应用程序类中,我不断收到错误:

In writing a BookStoreApplication which uses Book, Tape, and CD classes to create objects. Although unfinished, the application class should create new BookStoreItems, which are Book, Tape, and CD. They inherit from the BookStoreItems class. In this application class I keep getting the error:

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've changed it to be static and then not to be static, yet I continue to get 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("
" + i + "	Enter 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("
Invalid choice.");
            }//end of switch statement

        }//end of for loop

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

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


    }//end of main method


//PRINT MENU----------------------------------------------------------  
    public void printMenu(){
        System.out.println("
Press:");
        System.out.println("	" + ADD_BOOK + "	To add a book to the book store.
");
        System.out.println("	" + ADD_TAPE + "	To add a tape to the book store.
");
        System.out.println("	" + ADD_CD + "	To add a CD to the book store.
");
        System.out.println("	" + QUIT + "	To exit
");
    }
//---------------------------------------------------------------------
//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

推荐答案

你需要同时创建你的方法 - printMenu()getUserChoice() static,因为您直接从 static main 方法调用它们,而不创建类的实例,这些方法定义在.并且您不能调用 non-static 方法,但不引用定义它们的类的实例.

You need to make both your method - printMenu() and getUserChoice() static, as you are directly invoking them from your static main method, without creating an instance of the class, those methods are defined in. And you cannot invoke a non-static method without any reference to an instance of the class they are defined in.

或者,您可以将方法调用部分更改为:

Alternatively you can change the method invocation part to:

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

这篇关于不能从具有方法和变量的静态上下文中引用非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆