无法从同一个类中的静态上下文引用非静态方法

时间:2015-03-30 17:35:26

标签: java oop static non-static

我已经完成了大部分学习注册界面的代码,但是我收到了这个错误。

  

无法从静态上下文引用非静态方法。

现在,我知道这种问题很常见,但它仍然没有点击,我发现的任何一个似乎都没有与我自己的代码相似,以便自己找到解决方案,因为这个错误正在发生with with methods in同一类的另一个方法。

以下是整个代码,主要区域标有注释:

import java.util.Scanner;

public class RegistryInterface 
{
   private Registry theRegistry = null;

   public RegistryInterface(Registry theRegistry)
   {

   }

   public void doMenu()
   {
       boolean menuClose = false;

       while (menuClose != true)
       {
           Scanner in = new Scanner(System.in);
           System.out.println("Registry Main Menu");
           System.out.println("******************");
           System.out.println("");
           System.out.println("1) Add a Student");
           System.out.println("2) Delete a Student");
           System.out.println("3) Print Registry");
           System.out.println("4) Quit");
           System.out.println("Select option [1, 2, 3, 4] :> ");
           int option = in.nextInt();

           if (option == 1)
           {
               boolean anotherStudent = false;
               while (anotherStudent != true)
               System.out.println("");
               System.out.println("Add New Student");
               System.out.println("***************");
               System.out.println("");
               System.out.println("Enter forename      :> ");
               String aForeName = in.nextLine();
               System.out.println("Enter surname       :> ");
               String aSurName = in.nextLine();
               System.out.println("Enter student ID    :> ");
               String aID = in.nextLine();
               System.out.println("Enter degree scheme :> ");
               String aDegreeScheme = in.nextLine();
               RegistryInterface.doAddStudent(aForeName, aSurName, aID, aDegreeScheme); // static error here
               System.out.println("Enter another (1 for Yes/2 for No) :> ");
               int nextStudent = in.nextInt();
               if (nextStudent == 1)
               {
                   anotherStudent = false;
               }
              else if (nextStudent == 2)
              {
                   anotherStudent = true;      
              }

              else
              {
                   System.out.println("No correct number entered, exiting menu...");
                   anotherStudent = true;
              }
          }

        if (option == 2)
        {
            boolean anotherStudent2 = false;
            while (anotherStudent2 != true)
            {
                System.out.println("");
                System.out.println("Delete a Student");
                System.out.println("****************");
                System.out.println("");
                System.out.println("Enter Student ID    :> ");
                int studentID = in.nextInt();
                RegistryInterface.doDeleteStudent(studentID);  // Static error here.
                System.out.println("Delete another (1 for Yes/2 for No) :> ");
                int nextStudent2 = in.nextInt();
                if (nextStudent2 == 1)
                {
                    anotherStudent2 = false;
                }
                else if (nextStudent2 == 2)
                {
                    anotherStudent2 = true;
                }

                else
                {
                    System.out.println("No correct number entered, exiting menu...");
                    anotherStudent2 = true;
                }
               }

               if (option == 3)
               {
                   System.out.println("");
                   RegistryInterface.doPrintRegistry();  // static error here.
               }

               if (option == 4)
               {
                   menuClose = true;
               }
           }
       }
   }

   private void doAddStudent(String aForeName, String aSurName, String aID, String aDegreeScheme)
   {
       theRegistry.addStudent(new Student(aForeName, aSurName, aID, aDegreeScheme));
   }

   private void doDeleteStudent(int studentID)
   {
       theRegistry.deleteStudent(studentID);
   }

   private void doPrintRegistry()
   {
       System.out.println(theRegistry.toString());
   }
}

因此,对于第一个错误,它将是:

  

非静态方法doAddStudent(String,String,String,String)不能从静态上下文中引用。

解决此问题的最简单方法是什么?如果需要,可以提供其他详细信息。

1 个答案:

答案 0 :(得分:1)

变化

RegistryInterface.doDeleteStudent(studentID);  // Static error here.

 this.doDeleteStudent(studentID);

希望帮助你!