How to transform attributes into properties?

Asked

Viewed 329 times

7

In C#, I can avoid using getter and setter, transforming the attributes into properties, as below:

public class Pessoa {
    public int pes_idade { get; set; }
    public string pes_nome { get; set; }
}

You can do this in Java?

  • 3

    There is the Lombok replacing getter and Setter by Annotation.

  • This one and the official website lombok.

  • Did any of the answers solve your problem? Do you think you can accept one of them? If you haven’t already, see [tour] how to do this. You would help the community by identifying the best solution for you. You can only accept one of them, but you can vote for any question or answer you find useful on the entire site.

2 answers

6

In C# you create property that is nothing more than a private field (and not, attribute which is something else in C#) with a couple of methods (can be one) that will give access to this field. You don’t see them as methods (it’s syntactic sugar) but it is what it is. It would be basically this:

public class Pessoa {
    private int idade;
    public int getIdade() { return idade; }
    public void setIdade(int value) { idade = value; }
    private String nome;
    public String getNome() { return nome; }
    public void setNome(String value) { nome = value; }
}

I put in the Github for future reference.

In Java it does not have this facility, so you have to do it in hand, just like this. Unless you use the Lombok (see comments). I don’t see a lot of people doing it, there must be a reason (it may be just ignorance, but it seems that’s not all). See What is the Lombok?

Functioning of that standard

This creates an indirect that is useful for dynamic versioning and loading of modules, because the logic of the access passes to the method, so whenever you update the method in the class, everyone who call it will receive the new execution, even if nothing that does access has been recompiled (only the component of the changed class has been recompiled).

If you used the direct field, that wouldn’t be possible because the access is done directly. A change in this class required the recompilation of the entire application to ensure that access was done according to the new functioning.

So in some cases using the field is not a problem, even if in the future you need to switch to a method that has a processing. But if you’re not sure that a change can affect the entire application, then it’s best to make sure by already putting a method that does a very basic operation, which is the property in C#, or the pattern of getter and Setter in Java.

I’m not a fan of the term attribute, I prefer field.

  • It is possible for a programmer to implement this "facility" in java using what he has today, or the effort to do so is not worth it, given that it is more a "visual effect" on the code?

  • No. What you can do is create something in the IDE (some already have something) that makes it easy for you to enter the field and it creates the methods for you. But the methods will be visible in your code, risk being modified, etc.

  • https://projectlombok.org/features/GetterSetter.html

  • 1

    @diegofm look here, Gabriel has shown that he has a tool that helps in this. I don’t know how it works but either you need an external tool that does a pre-build, or you do a code injection or it has a cost of Runtime to deal with this.

  • @Bigown Actually the Ombok kind of kidnap compiler to do his job. I’m not sure, but if I’m not mistaken, in javac, he uses tricks of ClassLoaders to load modified versions of the internal javac classes, changing the compiler behavior. In ecj, he uses an agent to instrumentate the compiler classes through preMain, and then also change the functioning of the compiler.

-1

In Java it’s like this:

public class Pessoa {

    private String nome;
    private int idade;


    public String getNome() {
        return nome;
    }

    public int getIdade() {
        return idade;
    }

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

    public void setIdade(int idade) {
        this.idade = idade;
    }

}
  • 1

    The AP question is about a feature that allows you to use class attributes as properties, gettes and setters serve to encapsulate attributes.

  • I don’t think you understand (or haven’t read) the question. He wants to know if you can write getters and setters similar (the syntax) to c#

  • 1

    Mind the question, dear.

Browser other questions tagged

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