Don't use set and get methods in Java when there are many attributes

So I learned about OOP. I am currently trying to create simple "Hero Profiles" using HP, Mana, Intelligence, Strength, Maneuverability, Armor, etc. As you can see, there are many attributes that I want to add to Hero objects. But these attributes are declared private not public (because I was told it is better to declare them private), and when I declare them private, I have to create a set and get methods for each of these attributes. And it consumes so much time, and I feel that it is not practical.

Is there a more practical way? Do coders really introduce these get and set methods even though there are a dozen of them? All I want to do is create two hero profiles with these attributes, and then simulate a 1v1 battle between them.

+3


source to share


6 answers


Whoever told you that the individual attributes of an object must be private is absolutely right!

One of the basic principles of OOP is Encapsulation . In simple terms, this means that the internal structure of the object ("fields") must be hidden. This is the most important aspect of OOP, from which most of the benefits of OOP derive.

Now accessor methods (setters and getters) break encapsulation , the same as making them public, since you no longer hide the internal structure. This is not only the protection of internal fields, but also their hiding.

Unfortunately, accessor methods are a common idiom in Java (and C # too), but if you're serious about learning OOP, you should avoid them at all costs! Later, when you have more experience, you can make exceptions in a few cases, but I would highly recommend that you just avoid them for now. Avoid Project Lombok and avoid the IDE's automatic file generation capabilities.

Here is a brilliant article from Allen Golub on the subject.



Back to your actual question: Yes, you declare your fields private, but you do not define setters / getters for them. Instead, we think about what functions / functions / functions / logic objects should have.

So instead of thinking about statistics, think about what you are using those statistics for. An Object Oriented Hero might look like this:

public final class Hero {
    private int hp;
    private int strength;
    private int agility;
    private int armor;
    ...

    public void dealDamageTo(Hero other) {
        other.takeDamage(stength);
    }

    public void takeDamage(int damage) {
        hp = hp - Math.max(0, damage-armor);
    }
    ...
}

      

Of course, this is just an example, but you probably get the point. There is no need to post internal margins at all if your design is correct.

+2


source


You have important getters ans settings around your properties. Otherwise, you are breaking encapsulation.



Unfortunately Java does not provide a suitable shortcut for programmers to declare getters and setters. However, many IDEs provide tools for creating accessor methods.

+2


source


Yes, you should write or generate getters and setters, thanks to your IDE to encapsulate your fields, it is more than good practice you should not change the get () and set () calls in your code when the variable name or type changes (for example, from collection to list), and more useful in java8 for using lambda methods call

In addition, your class manages its attributes and no one can change its state in an inconvenient way.

+1


source


If you hate creating getters and setters, or if you think you are confusing / unnecessary, you can use Lombok. Maybe it's not a beginner to use external frameworks, but it's interesting.

https://projectlombok.org/

check it:

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

https://projectlombok.org/features/Data.html

+1


source


@huzo, Welcome to OOP and Thanks for trying Java. Answering your question, for IntelliJ use https://www.jetbrains.com/help/idea/2017.1/generating-getters-and-setters.html , and once you get familiar, try https://projectlombok.org/ . as suggested by @Rjiuk to avoid setters and getters.

+1


source


I am currently trying to create simple "Hero Profiles" using HP, Mana, Intelligence, Strength, Agility, Armor, etc. As you can see, there are many attributes that I want to add to Hero objects.

Looking at this line, I can tell: here you have an inheritance tree. You can take a parent class for them and store common members (variables and methods) in it. The parent class can be interface

either abstract class

or a concrete class. So, you can pass values ​​for member variables through the constructor.

In fact, you think you have a class strength

and you have a member variable called armStrength

(as an example). When you create a new instance of this class, you can pass the value through the constructor. But later the hand became weak / broken. In this case, you need to update armStrength

.

You need a setter method for this. Having getter and setters is the best thing about OOP. Due to encapsulation, it is one of the main points in OOP.

To avoid confusion, when you have many getters and setters, you can keep them in a separate class.

Fortunately, instead of injecting all the getters and setters, you can use IDE shortcuts to generate them.

  • In Eclipse: Alt+Shift+S, R

  • In Netbeans: Alt+Insert

  • In IntelliJ: Alt+Insert

+1


source







All Articles