Huzo
Huzo

Reputation: 1692

Not using set and get methods in Java when there are many attributes

So I have been learning about OOP. I am currently trying to create simple "Hero Profiles" with HP, Mana, intelligence, strength, agility, armor etc. As you can see, there are a lot of attributes I want to add to Hero objects. But these attributes are declared in private not public (Because I was told that it was best to declare these private) and when I declare them in private I have to form set and get methods to each of these attributes. And it consumes so much time and I feel like it is not practical.

Is there a more practical way? Do coders really type 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 the two.

Upvotes: 3

Views: 680

Answers (6)

Robert Bräutigam
Robert Bräutigam

Reputation: 7744

Whoever told you, that individual attributes of the object should be private was absolutely right!

One of the basic principles of OOP is Encapsulation. Simply put, it means that the internal structure of the object (the "fields") should be hidden. This is the most important aspect of OOP, from which most of the advantages of OOP come from.

Now, accessor methods (setters and getters) violate encapsulation, the same way just making them public does, since you are no longer hiding the internal structure. It is not just about protecting internal fields, it's about hiding them.

Unfortunately accessor methods are a common idiom in Java (and C# too), but if you are serious about learning OOP, you should avoid them at all costs! Later, when you have more experience, you might make exceptions in a few cases, but I would urge you just avoid them for now. Avoid Project Lombok and avoid IDE features to auto-generate stuff.

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

Back to your actual question: Yes, you declare your fields private, but you don't define setters/getters for them at all. What we do instead, is think about what responsibilities/features/functions/logic the object should have.

So instead of thinking about the stats, think about what you are using those stats for. An object-oriented Hero might look like this then:

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, that's only an example, but you probably get the point. There's no need at all to publish the internal fields at all, if your design is right.

Upvotes: 4

Blasanka
Blasanka

Reputation: 22437

I am currently trying to create simple "Hero Profiles" with HP, Mana, intelligence, strength, agility, armor etc. As you can see, there are a lot of attributes I want to add to Hero objects.

Looking at these line I can say, Here you have inheritance tree. You can take a parent class for these and keep common members(variables and methods) in it. Parent class can be interface or abstract class or concrete class. So then you can pass values for member variables through the constructor.

In fact, think you have strength class and you have member variable called armStrength(as a example). When you create new instance of this class you can pass value through the constructor. But later on arm become weak/broken. In this case you need to update armStrength.

To accomplish that also you need a setter method. Having getter and setters is a better thing in OOP. Because of encapsulation is one of the main point of an in OOP.

In order to avoid confusion when having lot of getters and setters you can keep those in a separate class.

Luckily, rather than typing all the getters and setters you can use IDE shortcuts to genetate those for you.

  • In Eclipse: Alt+Shift+S, R
  • In Netbeans: Alt+Insert
  • In IntelliJ: ALT+Insert

Upvotes: 1

Yogendra Mishra
Yogendra Mishra

Reputation: 2609

@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 become familiar do try out https://projectlombok.org/ as suggested by @Rjiuk to avoid setters and getters.

Upvotes: 1

Emil Hotkowski
Emil Hotkowski

Reputation: 2343

If you hate to make getters and setter, or you think they create confusion/ they are unnecesarry, you can use Lombok. Maybe it is not a beginner thing to use external frameworks but it is interesting.

https://projectlombok.org/

check this :

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

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

Upvotes: 1

D. Peter
D. Peter

Reputation: 517

Yes, you should write or generate getters and setters thanks to your IDE to encapsulate your fields, moreother than a good practice, you haven't to change the get() and set() calls in your code when you change the variable's name, or type (from Collection to List for instance), and it's more usefull in java8 for the use of lambda methods call

Plus, YOUR class is managing its attributes, and no one can change their state in an unconvinient way

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

Having getters ans setters around your properties is really important. Otherwise, you break encapsulation.

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

Upvotes: 2

Related Questions