English 中文(简体)
“非静态方法不能从静态情形中援引”错误,不可靠[重复]
原标题:A "non-static method cannot be referenced from a static context" error appears, unsure [duplicate]
  • 时间:2011-10-22 19:43:36
  •  标签:
  • java
  • static
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
What is the reason behind “non-static method cannot be referenced from a static context”?

public void Sort(){
*some code*
}
public void displayResults()
  {*more code*
}

public static void main(String[] args)
{
Sort();
displayResults();
}

我为什么会发现这一错误? 我有这种看法;在另一个抽象的类别中,这一类别正在扩大。

- 引信

最佳回答

You need to instantiate the class that contains Sort(), displayResults() and main before you can call Sort() or displayResults() from main().

class Example {
    public void Sort(){
        // *some code*
    }
    public void displayResults()
    {
        // *more code*
    }
    public static void main(String[] args)
    {
        Example ex = new Example()
        ex.Sort();
        ex.displayResults();
    }
}
问题回答

你们需要一个称为非统计方法的班级。 从静态方法来看,你不曾有过,因为静态与某类人有关,而不是与一种情况有关。 因此,不允许你在静态环境下使用非静态方法或获取非静态变量。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签