Getters and Setters in Java

-1

Is there any method of annotation in Java 8 (or earlier) to have at least one of the POX classes in the Getters and Setters methods without the need to write them? I speak of something similar to what in C# is done like this:

public string Code { get; set; }
public string Name { get; set; }
public string Details { get; set; } 

I've seen that there are third-party libraries to do it, like the Project Lombok , but I'm looking to see if it can be done directly with the Java JDK

UPDATE : I'm not looking for how to write getters and setters, but quite the opposite, how not to write them, in the style of C #

    
asked by jjmartinez 06.04.2017 в 12:35
source

5 answers

4

You can do what you need from Java, but not natively you must download the "lombok" library to add it to your project and then add the annotations (@Getter @Setter) to your variables.

link lombok project: link

    
answered by 06.04.2017 / 16:12
source
3

Personally I used the library lombok , it has been very useful for me, it allows to generate the getters and setters through the use of annotations. For example:

@Getter @Setter private int code;

This is the link:

  

link

On its website there is a video that explains its use, the documentation is quite clear and has support for several IDEs. I hope you find it useful.

    
answered by 06.04.2017 в 16:08
0

The truth does not exist estimated.

There is only the fact of defining variables, and then placing the construction through right click, insert code and appear construction of Getters and Setters.

Now, I do not understand what your goal is, since in C # the code is shorter and in java more extensive, but both are quick to create considering that technologies have been made that aim at the rapid creation of these objects.

The following image although as you said, you do not have a smaller code, it visually helps you to see that everything is coupled, which makes it easier to see. But as I say again, there is no what you formulate in JDK.

Greetings!

    
answered by 06.04.2017 в 13:11
0

Write a property of the form string Code { get; set; } when neither the method get nor the set contribute any variation to the behavior (they simply return the value of the variable), it is not far to establish the variable as public and obtain or modify its value directly.

Responding to your question: The simplest way to simulate the notation of propiedad of C# in JAVA , when the methods get and set act by default, is to declare the field as public .

Encapsulation is broken if all fields are public or if all fields have get and set by default.

    
answered by 06.04.2017 в 13:41
-3

Although it is not the solution to your problem, if you do a rightclick on the code, Source > > Getters and Setters, you can generate them

I'm sorry I do not know the exact solution, but maybe this helps

    
answered by 06.04.2017 в 13:13