3

I've got this Setting.class and it has a few variables.

private int timeRemember, timeConcentration;
private boolean hearts, spades, diamonds, clubs;

I know that I should create getters and setters for them but it will take like 50 lines of code. If I had more variables, let's say 40, it would be like 200 or so.

My question is - everyone does that? Or there's something I don't know?

Bradley Wilson
  • 1,197
  • 1
  • 13
  • 26
mafiozorek
  • 49
  • 6
  • 2
    what IDE are you using??. you can generate all the getters and setters for your fields. – Ousmane D. Mar 17 '17 at 14:59
  • This might help as well https://projectlombok.org/ – Philippe Mar 17 '17 at 15:00
  • 1
    I know I can, I did that. I'm just curious if that's what people do. – mafiozorek Mar 17 '17 at 15:00
  • Yes, you probably should make get-methods and set-methods. They are not pointless—see http://stackoverflow.com/questions/1568091/why-use-getters-and-setters . – VGR Mar 17 '17 at 15:03
  • If you're asking about what others do, then your question is out of scope for SO. If you're asking about why it should be done, it's for https://en.wikipedia.org/wiki/Information_hiding – Jiri Tousek Mar 17 '17 at 15:05
  • 1
    What I do is not have classes with so many instance variables. Plus it is unlikely that it makes sense to set all the fields separately, bearing in mind that instances of the class must be in a valid and usable state at all times after construction. A similar argument can be done about getters: does this class really have to expose its entire internal representation to the entire world? More often than not the answer to both these questions is "no". – biziclop Mar 17 '17 at 15:06

4 Answers4

6

If you have 40 instance variables you have something else to worry about. Your class design most likely has a problem.

In general, anything more than 10-11 fields are considered too much.

In addition to that, you may also want to ask yourself if the class actually needs getters and setters? can it be immutable? etc..

epoch
  • 16,396
  • 4
  • 43
  • 71
0

Whether or not you need getters and setters for your class variables is up to you. It depends on how that class is used elsewhere in your code. If you aren't sure, you could always add the getters and setters as you find you need them during development.

However, many modern Java IDEs allow you to auto generate getters an setters (such as Eclipse or IntelliJ). In IntelliJ, you can do this using the Code > Generate... menu.

kunruh
  • 874
  • 1
  • 8
  • 17
  • 3
    I love how I get two down votes, but no comments. Can't see anything in my answer that contradicts epoch's with exception to not mentioning class design. – kunruh Mar 17 '17 at 15:28
  • I was about to suggest that instead of having fields, a single Map could hold all the values inside the class with keys defined as final variables. Then I realised that I'd be downvoted to hell just for opening that possibility. There is always a "smart" guy around ready to point the "right" way to do things without any good reason. – Serg M Ten Mar 17 '17 at 16:17
0

You can take advantage of things such as Enums (instead of a bool for each suit), build classes with a single responsibility (separate time and card), and ask yourself if you really need to expose that variable outside of your class.

Trevor
  • 1
0

Take a look at project lombok. It allows you to, among many other things, "automate" setter/getter creation with annotations.

Justas
  • 811
  • 1
  • 11
  • 22