2015-12-03 78 views
1

我有一个名为AttachmentsBean的类,它有一个名为showUploadDialog()的方法。在名为UploadBean另一个类,当我执行下面的代码:不能从静态上下文中引用不同类的非静态方法

if(count=0) 
{ 
    return AttachmentsBean.showUploadDialog(); 
} 

我得到的错误:

"Non-static method cannot be referenced from a static context".

请建议。

回答

1

AttachmentsBean.showUploadDialog()只有在showUploadDialogstatic修饰符一起声明时才适用。

1

showUploadDialog(的签名)应该是这样的

public static <return type> showUploadDialog() { 
//Do something 
} 
0

您可以使用AttachmentsBean.showUploadDialog()只有showUploadDialog声明为静态:

public static ... showUploadDialog() { 
    ... 
} 

,如果你需要调用NO-静态方法,您首先需要创建AttachmentsBean对象,例如:

if(count=0) 
{ 
    return new AttachmentsBean().showUploadDialog(); 
} 
相关问题