Java 静态上下文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4129706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:54:18  来源:igfitidea点击:

Java static context

javastatic

提问by pie154

I am using a package that has a method call that is non-static. It will not let me call this method from a static context. I can't change the non-static method, how can I call this method?

我正在使用一个具有非静态方法调用的包。它不会让我从静态上下文中调用此方法。非静态方法改不了,怎么调用这个方法?

回答by BoltClock

Create an object out of that class and call the method on the object?

从该类中创建一个对象并调用该对象上的方法?

import com.acme.myclass;

...

MyClass obj = new MyClass();
obj.nonStaticMethod();

If the package you're using has any documentation, be sure to look through it to see how you're expected to use that class and its non-static method. You may also want to read up more on static versus non-static in object-oriented programming in general, to get a better idea of the differences.

如果您使用的包有任何文档,请务必仔细阅读以了解您希望如何使用该类及其非静态方法。您可能还想阅读更多关于一般面向对象编程中静态与非静态的内容,以更好地了解差异。

回答by jjnguy

In order to call a non-static method, you must call the method on an instance of an object.

为了调用非静态方法,您必须在对象的实例上调用该方法。

Given the following class:

鉴于以下类:

public class MyClass {
    public void nonStaticMethod();
}

You would call the method like so:

您可以像这样调用该方法:

new MyClass().nonStaticMethod();

Or, if you need to call that method more than once, you can save it into an object.

或者,如果您需要多次调用该方法,您可以将其保存到一个对象中。

MyClass instance = new MyClass();
instance.nonStaticMethod();
...
instance.nonStaticMethod();

回答by paxdiablo

You can instantiate an object of the class whenever you need to call the non-static method, with something like:

您可以在需要调用非静态方法时实例化类的对象,例如:

new BadlyWrittenClass().BadlyWrittenMethod();

However, if you're going to be doing this a lot, it may become inefficient to keep creating and destroying objects in that manner.

但是,如果您要经常这样做,那么继续以这种方式创建和销毁对象可能会变得效率低下。

A better way may be to instantiate oneobject, such as in your own class constructor, and just use that any time you need to call the method. Provided it doesn't require a newly initialised object each time, that's likely to be more efficient.

更好的方法可能是实例化一个对象,例如在您自己的类构造函数中,并在需要调用该方法的任何时候使用它。如果每次不需要新初始化的对象,那可能会更有效率。

But you may also want to keep in mind that there may be a reasonwhy the method is not static (despite my not-so-subtle jab in the class and method names above). Make sure that it doesn't require some state that you're not setting up when you're creating a new instance. In other words, don't blindly try to do this without understanding.

但是您可能还想记住,该方法不是静态的可能是有原因的(尽管我在上面的类和方法名称中不太巧妙)。确保它不需要您在创建新实例时未设置的某些状态。换句话说,不要在没有理解的情况下盲目地尝试这样做。

回答by OscarRyz

That method belongs to a class.

该方法属于一个类。

So, what you need to do is to create an instance of that class ( most likely with the new operator ) and then use it:

因此,您需要做的是创建该类的实例(最有可能使用 new 运算符),然后使用它:

package a;

class A {
    public void theMethod(){
    }
}

.....
package b;
import a.A;
class Main {
    public static void main( String [] args ) {
        A a = new A();
        a.theMethod();
     }
}

回答by fastcodejava

Non-static (instance) cannot be called from static context. Other way is possible.

不能从静态上下文中调用非静态(实例)。其他方式也是可能的。

回答by Alex

Static methods don't need to be instantiated whereas instance methods do, inside an instance class.

静态方法不需要实例化,而实例方法需要在实例类中实例化。

To get to an instance method you first need an instance of it's class using the newkeyword. You can then access this class' instance methods.

要使用实例方法,您首先需要使用new关键字的类的实例。然后您可以访问此类的实例方法。