在非静态类中调用静态方法时,是否实例化了类? [英] Is a class instantiated when a static method is called in a non-static class?

查看:126
本文介绍了在非静态类中调用静态方法时,是否实例化了类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bar 类中调用 Foo.SomeCheck()时,会发生什么情况?是否创建了 Foo 的实例以便调用 SomeCheck()?如果是这样,此实例是否存储在堆上,并且是否曾经通过垃圾回收进行收集?

Exactly what happens when Foo.SomeCheck() is called in the Bar class? Is an instance of Foo created in order to call SomeCheck()? If so, is this instance stored on the heap, and is it ever collected through garbage collection?

public class Foo() {
    public static bool SomeCheck() {
        return true;
    }
}

public class Bar() {
    public void SomeMethod() {
        // what happens when we access Foo to call SomeCheck?
        if (Foo.SomeCheck()) {
            //do something
        }
    }
}

推荐答案

静态方法与实例方法的不同之处在于,没有所属类的实例需要被创建才能被调用.当您调用静态方法时,实际上您是使用类型的名称而不是类型的实例进行调用的-这应该加强了在实例上不调用静态方法的想法. 需要重复和强调:不需要任何类的实例来调用该类的公共静态方法.

Static methods differ from instance methods in that no instance of the class they belong to needs to have been created for them to be called. When you call a static method, you in fact make the call using the name of the type rather than an instance of the type - which should reinforce the idea that static methods are not called on instances. That bears repeating and emphasis: No instance of a class is required to call a public static method of that class.

现在,您的示例格式不正确,但是大概是行:if( Foo.SomeCheck() )使用类型名称Foo-不是实例来调用SomeCheck静态方法. 但是,必须实例化Bar才能进行此调用-但是,在您的示例中,您没有格式正确的Bar实例.代码通常必须存在于方法(或成员初始化器)中-您在这里没有.

Now, your example is malformed, but presumably, the line: if( Foo.SomeCheck() ) is calling the SomeCheck static method using the name of the type: Foo - not an instance. Bar however, has to be instantiated in order to make this call - however, in your example, you don't have a well-formed instance of Bar. Code generally has to exist inside a method (or a member initializer) - which you don't have here.

回答您问题的其他部分.假设所讨论的代码是实例方法的一部分,则必须实例化Bar-并调用该方法. 某物将必须创建或获取Bar的实例.引用类型将始终在堆上创建-但这在很大程度上无关紧要.

To respond to the other parts of your question. Assuming the code in question is part of an instance method, something has to instantiate Bar - and invoke that method. That something would have to create or otherwise acquire an instance of Bar. Reference types will always be creted on the heap - but that's largely irrelevant here.

对于垃圾回收,您通常不必为此担心. .NET运行时确保清除程序中没有从任何根对象引用的实例.根通常是驻留在调用堆栈上某个地方的实例,或由一种或另一种类型的静态成员引用的实例.由于我们在此处看不到任何创建或引用Bar的代码,因此不可能说何时被收集.例如,如果Bar是单例并存储在静态变量中的某个位置,则它可能会生存很长时间-可能是程序的整个生命周期.您可能真的不知道所有 all 操纵和管理Bar的代码.

As for garbage collection, you normally shouldn't worry about this. The .NET runtime makes sure to cleanup instances that are not referenced from any root object in your program. Roots are typically instances that reside somewhere on the callstack or are referenced by static members of one type or another. Since we don't see any code here that creates or references Bar it's impossible to say when it will be collected. For instance, if Bar is a singleton and stored somewhere in a static variable, it may live for a very long time - perhaps the entire lifetime of the program. You can't really know without seeing all of the code that manipulates and manages Bar.

这篇关于在非静态类中调用静态方法时,是否实例化了类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆