简体   繁体   English

从方法内部对非静态方法进行静态引用

[英]Making a static reference to a non-static method from inside the method

I understand the "Cannot make a static reference to the non-static method" error, but I came across with this:我理解“无法对非静态方法进行静态引用”错误,但我遇到了这个:

public class MyClass {

    public static void main(String[] args) {
        MyClass myClassInstance = new MyClass();        

        while (true) {
            myClassInstance.myMethod();
            myMethod();//Cannot make a static reference to the non-static method myMethod()
        }       
    }// END main

    void myMethod() {
        try {
            //Stuff 
            }
        } catch (Exception e) {
            myMethod();
        }
    }// END myMethod

}// END MyCLass

I can't just call myMethod() from main but I can do it from inside the method itself (in this case I want to call myMethod() again if an exception happens).我不能只从main调用myMethod()但我可以从方法本身内部调用(在这种情况下,如果发生异常,我想再次调用myMethod() )。

How does this work?这是如何运作的? is myClassInstance still somehow there because at that point I'm still inside myMethod() ? myClassInstance仍然以某种方式存在,因为那时我还在myMethod()
Would it be better to have static MyClass myClassInstance = new MyClass() at class level and then call myClassInstance.myMethod() every time?在类级别使用static MyClass myClassInstance = new MyClass()然后每次都调用myClassInstance.myMethod()会更好吗?

First of all you should know more about static and non-static methods:首先,您应该更多地了解静态和非静态方法:

A static method belongs to the class and a non-static method belongs to an object of a class.静态方法属于类,非静态方法属于类的对象。 That is, a non-static method can only be called on an object of a class that it belongs to.也就是说,非静态方法只能在它所属的类的对象上调用。 A static method can however be called both on the class as well as an object of the class.然而,静态方法既可以在类上也可以在类的对象上调用。 A static method can access only static members.静态方法只能访问静态成员。 A non-static method can access both static and non-static members because at the time when the static method is called, the class might not be instantiated (if it is called on the class itself).非静态方法可以访问静态和非静态成员,因为在调用静态方法时,类可能不会被实例化(如果它是在类本身上调用的)。 In the other case, a non-static method can only be called when the class has already been instantiated.在另一种情况下,非静态方法只能在类已经实例化时调用。 A static method is shared by all instances of the class.静态方法由类的所有实例共享。 These are some of the basic differences.这些是一些基本的区别。 I would also like to point out an often ignored difference in this context.我还想指出在这种情况下经常被忽视的差异。 Whenever a method is called in C++/Java/C#, an implicit argument (the 'this' reference) is passed along with/without the other parameters.每当在 C++/Java/C# 中调用方法时,都会传递一个隐式参数('this' 引用)和/不带其他参数。 In case of a static method call, the 'this' reference is not passed as static methods belong to a class and hence do not have the 'this' reference.在静态方法调用的情况下,“this”引用不会作为属于类的静态方法传递,因此没有“this”引用。

Reference : Static Vs Non-Static methods参考静态与非静态方法

How does this work?这是如何运作的? is myClassInstance still somehow there because at that point I'm still inside myMethod() ? myClassInstance仍然以某种方式存在,因为那时我还在myMethod()

myMethod is an instance method that belongs to myClassInstance . myMethod是属于myClassInstance的实例方法。 so when you call myMethod() inside myMethod() it's equivalent to this.myMethod()因此,当您在myMethod()调用myMethod() myMethod()它等效于this.myMethod()

Would it be better to have static MyClass myClassInstance = new MyClass() at class level and then call myClassInstance.myMethod() every time?在类级别使用static MyClass myClassInstance = new MyClass()然后每次都调用myClassInstance.myMethod()会更好吗?

When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.当我们将类的成员声明为静态时,这意味着无论创建了多少个类的对象,静态成员都只有一个副本。 A static member is shared by all objects of the class.静态成员由类的所有对象共享。 All static data is initialized to zero when the first object is created, if no other initialization is present.如果不存在其他初始化,则在创建第一个对象时,所有静态数据都将初始化为零。 For more information visit Static Variables : Good or Bad?有关更多信息,请访问静态变量:好还是坏? . .

I understand the "Cannot make a static reference to the non-static method" error, but I came across with this:我了解“无法对非静态方法进行静态引用”错误,但是遇到了这个问题:

public class MyClass {

    public static void main(String[] args) {
        MyClass myClassInstance = new MyClass();        

        while (true) {
            myClassInstance.myMethod();
            myMethod();//Cannot make a static reference to the non-static method myMethod()
        }       
    }// END main

    void myMethod() {
        try {
            //Stuff 
            }
        } catch (Exception e) {
            myMethod();
        }
    }// END myMethod

}// END MyCLass

I can't just call myMethod() from main but I can do it from inside the method itself (in this case I want to call myMethod() again if an exception happens).我不能只从main调用myMethod() ,而是可以从方法本身内部进行调用(在这种情况下,如果发生异常,我想再次调用myMethod() )。

How does this work?这是如何运作的? is myClassInstance still somehow there because at that point I'm still inside myMethod() ? myClassInstance仍然存在,因为那时我仍在myMethod()
Would it be better to have static MyClass myClassInstance = new MyClass() at class level and then call myClassInstance.myMethod() every time?在类级别具有static MyClass myClassInstance = new MyClass()然后每次都调用myClassInstance.myMethod()会更好吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM