简体   繁体   English

静态参考变量的行为

[英]Behaviour of the static reference variable

I have a static reference variable 我有一个静态参考变量

static IMail mailer = null;

to which I am assigning a value in the constructor of the class SearchManager 在类SearchManager的构造函数中为其分配值的对象

public SearchManager(ILog logger, String basePath, String indexPath, String nwId, IMail mailer) {
        this.logger = logger;
        this.basePath = basePath;
        this.indexPath = indexPath;
        this.nwId = nwId;
        this.mailer = mailer;
    } 

and I am using the mailer in my code. 我在代码中使用了邮件程序。 The constructor of this class might be called multiple times. 此类的构造函数可能被多次调用。 So I have a query that how this static reference variable will behave each time the constructor is called. 因此,我有一个查询,该静态引用变量在每次调用构造函数时将如何表现。 Will it take only the value which I passes when I called the constructor the first time or will it take different values each time I call the constructor? 是只接受我第一次调用构造函数时传递的值,还是每次调用构造函数时都取不同的值?

First of all you should not use 'this' keyword with static variable . 首先,您不应该在静态变量中使用'this'关键字 You can do the following(not recommended though) 您可以执行以下操作(虽然不建议这样做)

public SearchManager(ILog logger, String basePath, String indexPath, String nwId, IMail mailerarg) {
        this.logger = logger;
        this.basePath = basePath;
        this.indexPath = indexPath;
        this.nwId = nwId;
        mailer = mailerarg;
    } 

and Yes it being a static variable will change every time you create a new object of the Class .Static variables belong to Class rather than it's individual instances. 是的,它是静态变量,每次您创建Class的新对象时都会更改。静态变量属于Class而不是其单个实例。 So whenever you change it's value it will be reflected in all the corresponding class objects(All objects have their class information). 因此,无论何时更改它的值,它都会反映在所有相应的类对象中(所有对象都有其类信息)。

Will it take only the value which I passes when I called the constructor the first time or will it take different values each time I call the constructor? 是只接受我第一次调用构造函数时传递的值,还是每次调用构造函数时都取不同的值?

It will take different values as it is shared by the instances, and each instance will see the same value off course. 实例共享该值时将采用不同的值,并且每个实例在偏离航向时将看到相同的值。

As per the JLS 8.3.1.1 : 根据JLS 8.3.1.1

If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. 如果将一个字段声明为静态,则无论该类最终会创建多少实例(可能为零),都只存在该字段的一个具体化身。 A static field, sometimes called a class variable, is incarnated when the class is initialized 初始化类时,会包含一个静态字段(有时称为类变量)

Will it take only the value which I passes when I called the constructor the first time or will it take different values each time I call the constructor? 是只接受我第一次调用构造函数时传递的值,还是每次调用构造函数时都取不同的值?

It will take different values each time you call the constructor. 每次调用构造函数时,它将采用不同的值。

Static variables are not particularly special. 静态变量不是特别特殊。 When you assign a value to variable, the variable takes that value. 为变量分配值时,变量将使用该值。 This is equally true of static variables, so if you assign a static variable multiple times, its value will be updated each time. 对于静态变量也是如此,因此,如果多次分配静态变量,则每次都会更新其值。


In this case, if you're calling a contructor (potentially) multiple times with different arguments, it sounds very much like this should be an instance-spceific field, rather than a single static variable. 在这种情况下,如果要使用不同的参数多次(潜在地)调用构造函数,听起来很像这应该是实例专用字段,而不是单个静态变量。 That way, each SearchManager has a reference to the mailer that it was constructed with, rather than the mailer of the manager that happened to be constructed most recently. 这样,每个SearchManager都具有对与其一起构造的mailer的引用,而不是对最近构造的管理者的邮件的引用。

Even if you don't agree with this, and feel that there should be a single mailer for the whole application, I would suggest you change the way it's currently managed. 即使您不同意这一点,并且认为整个应用程序应该只有一个mailer程序,我还是建议您更改当前管理它的方式。 Values passed into constructors tend to be used for that instance. 传递给构造函数的值通常用于实例。 Setting static variables in constructors is thus potentially very confusing, and it would be better to make an explicit call to something like MailerSupport.setGlobalMailer(mailer) instead whenever you wanted to change the value. 因此,在构造函数中设置静态变量可能会造成很大的混乱,因此最好是显式调用MailerSupport.setGlobalMailer(mailer)而不是在您想更改值时进行调用。

All instances of an object share static data. 对象的所有实例共享静态数据。 Thus, all Insatnces(your calls to SearchManager constructor) would share mailer. 因此,所有Insatnces(您对SearchManager构造函数的调用)都将共享邮件。

ie, say first call to SearchManager had value "X" for mailer and another call to SearchManager would set value "Y" for mailer, now all the instances would have value Y for mailer. 也就是说,假设对SearchManager的第一次调用对于邮件程序具有值“ X”,而对SearchManager的另一次调用将针对邮件程序设置值“ Y”,现在所有实例对于邮件程序都将具有值Y。

您可以多次更改其值,但所有实例将看到相同的值。

值可以随时更改,但是所有实例都具有相同和最新的值。即上一次调用

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

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