Difference between "Attribute" and "Instance Variable"


Reading some books and articles on introducing Java and OO I noticed 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 does this influence during the programming process?

asked by Vinicius 06.11.2015 в 14:42

3 answers


The terms are almost even interchangeable.

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 does not even appear in the documentation .

Where you are reading the attribute, start reading the field, because that is what you are saying when we speak in programming languages. For the languages attribute is something else unrelated to what is being spoken here . It is the story of the lie repeated so many times that it happened "to be true."

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

Some people prefer to use "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 of this class. Class variables are the attributes fields that are present in the class itself and are shared by all objects in it.

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

A field can belong to the class or instance. A field always uses a variable (or constant) as a mechanism (I've never seen it differently). Then there are instance variables (belongs to the object) and class variables (belongs to the class).


How does this influence during the programming process?

In programming itself does not influence anything. This influences the communication process. Everyone involved needs to understand what is being said. If people do not understand what is being said, they will not execute correctly, or they will do so by coincidence.

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 and it is your fault that did not use the correct term. If it created a static field it was also communication failure. If you use the terminology correctly, within the context, and the person uses it wrong, it is their fault that does not know the correct term.

Many books, blogs, and other sources use the term without thinking about the need for precision and the context that is being used. In many cases, it may not cause confusion, in other cases it does.

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

See What's the difference between a class and an object? .

06.11.2015 / 14:59
In Object Orientation Attribute is a property or also known as a field or a variant that are pertaining 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 attributes color, mark and name which 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 class Car, we could have several instances of this class where in each of them we would have their attributes defined differently as in the following example:

public class MeuCarro {

    public static void main(String[] args) {

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

        Carro carro2 = new Carro();
        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());


06.11.2015 в 15:26

Attributes can be seen as the properties of objects, 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 assigned to allocate the color of the car which can be, for example, blue.

08.06.2016 в 00:45