调用方法是静态的
static TheRegistryClass theRegistry;
static void callingMethod(){
/// Some code here
Scanner in = new Scanner (System.in);
char choice = in.next().charAt(0);
if(choice == 1)
{
doAddStudent(aStudent);
}
//remaining code here
}
if 块中调用的方法在同一类中但在调用方法之外声明
private static void doAddStudent(Student aStudent)
{
theRegistry.addStudent(aStudent); // static methods do not have reference to this
}
如果调用方方法是非静态的 TheRegistryClass theRegistry; void callingMethod(){ /// 这里有一些代码 Scanner in = new Scanner (System.in); 字符选择 = in.next().charAt(0);
if(choice == 1)
{
doAddStudent(aStudent);
}
//remaining code here
}
private static void doAddStudent(Student aStudent)
{
this.theRegistry.addStudent(aStudent); // this is optional
}