本文介绍了使用类实例化声明的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有Class1包含


静态Class2 var1 = new Class2();


如果var1只执行Class2构造函数代码在
代码执行路径中引用?


或者是在引用的任何类的加载时执行的Class2构造函数代码

Class1?


或者...别的...

Say I have Class1 which contains

static Class2 var1 = new Class2();

Is Class2 constructor code only executed if var1 is referenced in the
code-execution path ?

Or is Class2 constructor code executed on load of any class that references
Class1 ?

Or ... something else ...

推荐答案




我当然假设代码看起来像这样:


public Class1

{

静态Class2 var1 = new Class2();

}


这样var1实际上是Class1的静态成员。


Jon Skeet是实例化大师,但是如果内存服务于实例,那么将构造
的Class2并将其分配给var1第一次执行路径提到Class1,或者通过尝试创建

一个实例或者提到Class1的任何静态成员。


它与为Class1创建静态构造函数相同:静态

构造函数在第一个提及时运行。 Class1之前,可以访问

类中的任何内容。


我保留错误的权利。 :-)



I''m assuming, of course, that the code looks something like this:

public Class1
{
static Class2 var1 = new Class2();
}

So that "var1" is effectively a static member of Class1.

Jon Skeet is the instantiation guru, but if memory serves the instance
of Class2 will be constructed and assigned to var1 the first time that
the execution path makes mention of Class1, either by trying to create
an instance or mentioning any static member of Class1.

It''s the same as creating a static constructor for Class1: the static
constructor runs on first "mention" of Class1, before anything in the
class is accessible.

I reserve the right to be wrong, though. :-)




我当然假设代码看起来像这样:

公共Class1
{静态Class2 var1 = new Class2();

}

这样var1 ;实际上是Class1的静态成员。

Jon Skeet是实例化大师,但是如果内存服务于实例,那么第2次将构造并分配给var1
执行路径提到了Class1,要么试图创建一个实例,要么提到Class1的任何静态成员。

它与为Class1创建静态构造函数相同: static
构造函数首先在提及中运行。 Class1之前,可以访问
课程中的任何内容。

我保留错误的权利。 : - )



I''m assuming, of course, that the code looks something like this:

public Class1
{
static Class2 var1 = new Class2();
}

So that "var1" is effectively a static member of Class1.

Jon Skeet is the instantiation guru, but if memory serves the instance
of Class2 will be constructed and assigned to var1 the first time that
the execution path makes mention of Class1, either by trying to create
an instance or mentioning any static member of Class1.

It''s the same as creating a static constructor for Class1: the static
constructor runs on first "mention" of Class1, before anything in the
class is accessible.

I reserve the right to be wrong, though. :-)





我当然假设代码看起来像这样:

公共Class1
{静态Class2 var1 = new Class2();

}

这样var1 ;实际上是Class1的静态成员。

Jon Skeet是实例化大师,但是如果内存服务于实例,那么第2次将构造并分配给var1
执行路径提到了Class1,要么试图创建一个实例,要么提到Class1的任何静态成员。

它与为Class1创建静态构造函数相同: static
构造函数首先在提及中运行。 Class1之前,可以访问
课程中的任何内容。

我保留错误的权利。 : - )



I''m assuming, of course, that the code looks something like this:

public Class1
{
static Class2 var1 = new Class2();
}

So that "var1" is effectively a static member of Class1.

Jon Skeet is the instantiation guru, but if memory serves the instance
of Class2 will be constructed and assigned to var1 the first time that
the execution path makes mention of Class1, either by trying to create
an instance or mentioning any static member of Class1.

It''s the same as creating a static constructor for Class1: the static
constructor runs on first "mention" of Class1, before anything in the
class is accessible.

I reserve the right to be wrong, though. :-)



这篇关于使用类实例化声明的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 18:30
查看更多