Behaviour of the static reference variable

10,005

Solution 1

First of all you should not use 'this' keyword with static variable. 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. So whenever you change it's value it will be reflected in all the corresponding class objects(All objects have their class information).

Solution 2

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:

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

Solution 3

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.

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. 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.

Solution 4

All instances of an object share static data. Thus, all Insatnces(your calls to SearchManager constructor) would share mailer.

i.e, 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.

Share:
10,005
milind
Author by

milind

Updated on June 04, 2022

Comments

  • milind
    milind almost 2 years

    I have a static reference variable

    static IMail mailer = null;
    

    to which I am assigning a value in the constructor of the class 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?

  • Aniket Thakur
    Aniket Thakur almost 11 years
    Agreed it won't be an error because each object has it's class information but it is not correct usage. Also if you try auto-complete in IDE's like eclipse n IDEA you will not get the name of static variable after this.
  • Wayne
    Wayne almost 10 years
    Still, I'd recommend changing cannot to should not, primarily because it's false as written, which is not a desirable property of a good answer.