Difference between "Attribute" and "Instance Variable"

Asked

Viewed 8,723 times

9

Reading some books and articles about introduction to Java and OO I realized that these terms are not well defined. Looking deeper I found different definitions in different sources on the subject. What is the real difference between these types of variables? How this influences the programming process?

3 answers

10


Informally the terms are almost interchangeable even. To some extent.

The term attribute is widely used in modeling languages such as UML. And it is common for Java programmers to refer to variables as object attributes, but formally they are not. The term doesn’t even appear in the documentation.

Where you are reading attribute, start reading field, because this is what you are saying when we speak in programming languages. For languages attribute is another thing unrelated to what is being said here. It is the story of the lie repeated so many times that it became "true".

The term field is rarely used in the Java community, but it is present in the documentation linked above.

Some people prefer the use of "instance variables" to avoid confusion with other things that may use the term "attribute".

If they are instance variables they will be present in each object in this class. Class variables are already attributesfields that are present in the class itself and are shared by all its objects.

Strictly speaking, field is a more general term, as is "member" which includes class fields and methods.

A field can belong to the class or instance. A field always uses a variable (or constant) as mechanism (never seen different). So there are instance variables (belongs to object) and class variables (belongs to class).

How this influences the programming process?

In programming itself influences nothing, influences the communication process. Everyone involved needs to understand what is being said. If people do not understand what you are talking about they will not perform correctly, or they will do so by coincidence, so I hit the key of using the correct terms so much, it influences the end result. Some think it’s a silly theory, but it affects practice without the person realizing it.

If you want to create one thing and write another, then the person who received the information uses a method that does not solve the problem, it is your fault who did not use the correct term. If she created a static field, it was also a miscommunication. If you use the terminology correctly, within the context, and you use it wrong, it’s her fault that you don’t know the correct term.

Many books, blogs, and other sources use the term without thinking about the need for accuracy and the context being used. In many cases, it may not cause confusion, in others it even causes.

public class Exemplo {
    private String info1; //variável de instância que é um campo
    private static String info2; //variável de classe que é um campo
}

I put in the Github for future reference.

Behold What is the difference between a class and an object?.

2

In object orientation Attribute is a property or also known as field or variable that are belonging to a class, which are used to give characteristics to a class, as in the example below where we have the class Car with three color attributes, brand and name that are its characteristics in this example.

public class Carro {

    //Aqui são os atributos da classe Carro
    private String cor;
    private String marca;
    private String nome;

    public String getCor() {
        return cor;
    }

    public void setCor(String cor) {
        this.cor = cor;
    }

    public String getMarca() {
        return marca;
    }

    public void setMarca(String marca) {
        this.marca = marca;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

}

Instance variable as the name already says is an instance being created from a Class, following the same example of the Car class, we could have several instances of this class where in each of them we would have its attributes defined differently as in the following example:

public class MeuCarro {

    public static void main(String[] args) {

        Carro carro1 = new Carro();
        carro1.setCor("Azul");
        carro1.setMarca("Mitsubishi");
        carro1.setNome("Mitsubishi L200 Triton");

        Carro carro2 = new Carro();
        carro2.setCor("Preto");
        carro2.setMarca("Ford");
        carro2.setNome("Ford Ranger");

        System.out.println("Carro 1:");
        System.out.println("Cor: " + carro1.getCor());
        System.out.println("Marca: " + carro1.getMarca());
        System.out.println("Nome: " + carro1.getNome());

        System.out.println("\nCarro 2:");
        System.out.println("Cor: " + carro2.getCor());
        System.out.println("Marca: " + carro2.getMarca());
        System.out.println("Nome: " + carro2.getNome());

    }

}

0

Attributes can be seen as object properties, defined by their class. We can say that the car class defines a color attribute, which is the color of the car. When a car object is instantiated, an instance variable will be designated to allocate the color of the car that can be, for example, blue.

Browser other questions tagged

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