function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EdCodeEdCode 

Basic concept/question: what is the difference between "Attribute" and "Variable"

I have learnt the following about "Attributes" and "Variables". Could you please confirm if what I have learnt is correct or incorrect? (I am a bit confused with these two concepts now).
  • Attributes:
    • Are the "adjectives" or "parts" of a future object that is going to be constructed with a Constructor. I am guessing that we use the term "attribute" when we are, somehow, "designing" the object that will be constructed in the future.
    • Attributes are also called "properties"(?)
    • Example: we may be designing the future construction of a car (we design the car with a class "Car" where we specify all of its attributes which will be used by a Constructor which, in turn, will construct objects). When we design/define/specify these "parts" or "adjectives" , then I THINK that thats when we use the term "attributes". Right? (that's when we are still defining how objects are going to be)
    • An attribute, I think, is very similar to a Variable; the difference is that the attributes are to be used by a Constructor. Variables are not used in Constructors. Is that Right?
  • Variables:
    • Variables, I GUESS, are similar to attributes but they only start being called "Variables" when they have been declared as such which means they are capable of storing values. (attributes do not have this capability)
    • Once a Variable has been declared as a Variable, it becomes a "ready space in memory", ready to store values.

I am not sure if the above is 100% correct.
I suppose that what 's confusing me is the fact that the way Attributes are defined is similar to the way Variables are declared:

Here I define an attribute (should I use the term "define"? or...  some other term?):
mode type of NameOfClass nameOfAttribute;
Here below I declare a variable:
type or NameOfClass nameOfVariable;
I observe that, when defining an attribute, there is a "mode" (such as "private" or "public") but when declaring a variable, we do not include a "mode".

Could you possibly help me to clarify the conceptual difference between attribute and variable ?

Thank you.

 
Best Answer chosen by EdCode
Paul S.Paul S.
Well, yes and no.  In the above example, yes, you could say that it's because it was included in the constructor.  But it's more accurate to say that exteriorColor is considered an attribute because it's a non-static variable that is created/initialized with each instance of the class.  Another example:
public class NewCar {

    private String exteriorColor = 'Blue'; // This is an attribute of the NewCar class

    public void buildCar() {

        String interiorColor;  // This is a variable that we'll use to create a Car__c object

        if(exteriorColor == 'Red') {
            interiorColor = 'Tan';
        } else {
            interiorColor = 'Black';
        }       

       Car__c myCar = new Car__c(
            Name = 'My New Vehicle',
            Exterior_Color__c = exteriorColor,
            Interior_Color__c = interiorColor
        );

        insert myCar;

    }

}
When we execute this:
NewCar myCar = new NewCar();
System.debug(myCar);
We get the following in the debug log:

User-added image
Where you can see that even though we haven't specified a color in the NewCar myCar = new NewCar() line, our class definition automatically assigns the color blue because exteriorColor is an attribute of NewCar and is pre-set to "Blue."

Similarly, if we modify the class to also include interiorColor as an attribute:
public class NewCar {

    private String exteriorColor = 'Blue'; // This is an attribute of the NewCar class
    private String interiorColor; // Same here

    public void buildCar() {

        if(exteriorColor == 'Blue') {
            interiorColor = 'Tan';
        } else {
            interiorColor = 'Black';
        }       

       Car__c myCar = new Car__c(
            Name = 'My New Vehicle',
            Exterior_Color__c = exteriorColor,
            Interior_Color__c = interiorColor
        );

        insert myCar;

    }

}
And then execute this:
NewCar myCar = new NewCar();
System.debug(myCar);
We get this in the debug log, where now interiorColor appears, albeit as a null value.  (It's null because we haven't also called the method, buildCar(), that actually sets a color (this is slightly different from my very first example).  All we've done is created a new NewCar object.  It knows it has an attribute that is supposed to store the interior color, but we haven't set it yet.)

User-added image
If, however, you move exteriorColor and interiorColor within the buildCar method, they are no longer considered attributes:
public class NewCar {

    public void buildCar() {

    String exteriorColor = 'Blue';
    String interiorColor;

        if(exteriorColor == 'Blue') {
            interiorColor = 'Tan';
        } else {
            interiorColor = 'Black';
        }       

       Car__c myCar = new Car__c(
            Name = 'My New Vehicle',
            Exterior_Color__c = exteriorColor,
            Interior_Color__c = interiorColor
        );

        insert myCar;

    }

}
and executing
NewCar myCar = new NewCar();
System.debug(myCar);
results in the following debug log:

User-added image
Does that help?
 

All Answers

Paul S.Paul S.
Here's an example 
public class NewCar {

    //  exteriorColor is an attribute of the NewCar class; it's something about an instance of the 
    //  class that we define when we instantiate it.
      
    private String exteriorColor;

    public NewCar (String extColor) {  
        exteriorColor = extColor;
    }

    public void buildCar() {

        
        //  interiorColor is a variable that we'll use set the interior color of the Car__c object/record
        //  we're going to create.  It's not a characteristic of the class itself.
        
        String interiorColor;

        if(exteriorColor == 'Red') {
            interiorColor = 'Tan';
        } else {
            interiorColor = 'Black';
        }       

        Car__c myCar = new Car__c(
            Name = 'My New Vehicle',
            Exterior_Color__c = exteriorColor,
            Interior_Color__c = interiorColor
        );

        insert myCar;

    }

}
We'd then call this class like so:
NewCar myCar = new NewCar('Red');
myCar.buildCar();

 
EdCodeEdCode
Hi Paul, 

On your example of code (above), why interiorColor is NOT considered as an attribute? Is it because, interiorColor is not included as a parameter of the constructor?

InteriorColor here could well have been an attribute or a characteristic of the Class itself (you know, interior color is similar to external color, it is a characteristic of the car and it could be part of its blue print).

I need to make sure I undestand the distinction between Attributes an Variables and WHEN/WHY should we use one and/or the other.

Could you possible explain it to me in plain English?

Thank you very much.
Paul S.Paul S.
Well, yes and no.  In the above example, yes, you could say that it's because it was included in the constructor.  But it's more accurate to say that exteriorColor is considered an attribute because it's a non-static variable that is created/initialized with each instance of the class.  Another example:
public class NewCar {

    private String exteriorColor = 'Blue'; // This is an attribute of the NewCar class

    public void buildCar() {

        String interiorColor;  // This is a variable that we'll use to create a Car__c object

        if(exteriorColor == 'Red') {
            interiorColor = 'Tan';
        } else {
            interiorColor = 'Black';
        }       

       Car__c myCar = new Car__c(
            Name = 'My New Vehicle',
            Exterior_Color__c = exteriorColor,
            Interior_Color__c = interiorColor
        );

        insert myCar;

    }

}
When we execute this:
NewCar myCar = new NewCar();
System.debug(myCar);
We get the following in the debug log:

User-added image
Where you can see that even though we haven't specified a color in the NewCar myCar = new NewCar() line, our class definition automatically assigns the color blue because exteriorColor is an attribute of NewCar and is pre-set to "Blue."

Similarly, if we modify the class to also include interiorColor as an attribute:
public class NewCar {

    private String exteriorColor = 'Blue'; // This is an attribute of the NewCar class
    private String interiorColor; // Same here

    public void buildCar() {

        if(exteriorColor == 'Blue') {
            interiorColor = 'Tan';
        } else {
            interiorColor = 'Black';
        }       

       Car__c myCar = new Car__c(
            Name = 'My New Vehicle',
            Exterior_Color__c = exteriorColor,
            Interior_Color__c = interiorColor
        );

        insert myCar;

    }

}
And then execute this:
NewCar myCar = new NewCar();
System.debug(myCar);
We get this in the debug log, where now interiorColor appears, albeit as a null value.  (It's null because we haven't also called the method, buildCar(), that actually sets a color (this is slightly different from my very first example).  All we've done is created a new NewCar object.  It knows it has an attribute that is supposed to store the interior color, but we haven't set it yet.)

User-added image
If, however, you move exteriorColor and interiorColor within the buildCar method, they are no longer considered attributes:
public class NewCar {

    public void buildCar() {

    String exteriorColor = 'Blue';
    String interiorColor;

        if(exteriorColor == 'Blue') {
            interiorColor = 'Tan';
        } else {
            interiorColor = 'Black';
        }       

       Car__c myCar = new Car__c(
            Name = 'My New Vehicle',
            Exterior_Color__c = exteriorColor,
            Interior_Color__c = interiorColor
        );

        insert myCar;

    }

}
and executing
NewCar myCar = new NewCar();
System.debug(myCar);
results in the following debug log:

User-added image
Does that help?
 
This was selected as the best answer