【发布时间】:2015-03-30 17:35:25
【问题描述】:
我已经轻松完成了学生注册界面的大部分代码,但是我收到了这个错误。
不能从静态上下文中引用非静态方法。
现在,我知道这种问题很常见,但它仍然没有点击,而且我发现的问题似乎都与我自己的代码没有足够相似的关系,无法自己找到解决方案,因为这个错误正在发生在同一个类的另一个方法中使用方法。
这是完整的代码,主要区域标有注释:
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)。
解决这个问题的最简单方法是什么?如果需要,可以提供其他详细信息。
【问题讨论】:
-
这几乎就是错误消息所说的,doAddStudent 不是静态方法,但您试图将其称为 RegistryInterface.doDeleteStudent。您需要创建 RegistryInterface 的实例,或者将方法设为静态。
-
所有东西都在同一个类中。为什么需要静态方法?使其非静态并直接调用它。
标签: java oop static non-static