Why are instance variables usually initialized in the constructor?

Asked

Viewed 143 times

5

I have seen several codes where the instance variables of a class are initialized in the constructor, even I do it myself by watching others do it. But I never understood it.

Logical that has the instance variables in which it has the value defined based on the argument given in the constructor parameters, but I see most often instance variables being initialized with objects within the constructor.

An example (pseudocode):

class Exemplo {
    var obj;

    construtor() {
        obj = new Objeto();
    }
}

Is there a reason for this? Or has it just become a "custom"?

1 answer

4


First, you are using a wrong nomenclature, but it is not your fault, almost everyone uses wrong, see in What is the difference between attribute and field in the classes?.

The example is terrible because it is wrong, even if it were correct it would give problem because it would go into loop infinite.

Behold not working on ideone. And in the .NET Fiddle. Also put on the Github for future reference. Goes for any language.

I think that’s how you want to do it:

class Exemplo {
    var obj;
    Exemplo() {
        obj = new Objeto();
    }
}

A class or type definition similar to a class has two parts: one is the data structure which is practically a metadata which will indicate how the object structure will be, which fields will form this physical object; and the other which are the methods, that are but functions that will operate upon that structure.

Only the functions have algorithms, the fields even exist during execution, they serve only to guide what access, it is only a way to facilitate the coding of access to memory. Any operation to be executed must be in a function/method. So when you want to create an object in memory is an operation to be executed and needs to be within a method, and this method can be the constructor.

When you say obj = new Objeto() is calling the builder of Objeto, which is a method and is creating an object in memory, the new indicates that there will be an allocation there, and this object (probably a reference to it) will be assigned to the field obj of your class.

If you don’t do this what would happen in a language you let go free to do as you please? The field would not be initialized and would have any value according to what was already in the memory at that time.

Some languages do the memory zeroing at this point, but it does in a constructor even if it doesn’t look like it has one the language creates for you.

Some languages let you initialize the field directly in it, but in fact it’s just syntax sugar, at the bottom is creating a constructor and the boot is being put there. So:

class Exemplo {
    var obj = new Objeto();;
}

flipped:

class Exemplo {
    var obj;
    Exemplo() {
        obj = new Objeto();
    }
}

And the moment you have created an object Exemplo this constructor will be called and run this code. At the end of the data structure can not contain algorithm, so even if you see an execution in a data structure code actually it will be rewritten to play in the constructor without you knowing.

So if you just want the field to be initialized with a default value and it doesn’t have to be configured because it depends on parameter, you just initialize the desired value.

There’s a reason for this, whenever the code was written by someone who knows what they’re doing. And it’s just a custom if the person learned how to make a cake recipe and has no idea what he’s doing and still thinks he’s a programmer.

This is done if there is a need to solve the problem in this way, if what you want is an object with a specific value whenever you start a new object of this type, even if then that value can be exchanged within a method that makes that change (some people create a method Setter for this, and almost always not ideal to do). If there is no need do not use so. It is always the need that must determine what to do and not a universal magic rule.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.