5

I am writing a game and I have a class for the input which contains booleans for all the different keys. I create an instance of this class in the main game class. Is it ok for the booleans to be public, or should I access them with accessors?

user1150769
  • 395
  • 1
  • 4
  • 17

7 Answers7

4

Instead of having a boolean for each key, it would be more readable and easier to code if you had a private Map<String, Boolean> keyStates, with all keys initialized to false. Then your accessors might be:

public void setPressed(String keyName) {
    keyStates.put(keyName, true);
}

public void setReleased(String keyName) {
    keyStates.put(keyName, false);
}

public boolean isPressed(String keyName) {
    return keyStates.get(keyName);
}

The general reason for having accessor methods rather than public variables is that it allows the class to change its implementation without requiring changes in the classes that interact with its members. For example, with the above, you can now add code to count or log key presses, or change the underlying type of Map used, without exposing any of this to the outside.

This is not personal preference. Encapsulation and Interfaces are integral parts of OO Software Engineering, and are the primary design reasons that the Internet is possible from a technical POV.

calebds
  • 25,670
  • 9
  • 46
  • 74
3

Generally I would recommend using getters and setters as it is cleaner, more organized, and more readable. This will also help if you have a lot of different programmers looking at your code. My outlook is to always make your variables private unless you need to expose them for a specific reason. If performance is really an issue in your game then making your variables public will help a little by reducing function calls.

DRiFTy
  • 11,269
  • 11
  • 61
  • 77
3

It's mainly a personal taste thing - I'm sure you'll find people arguing on both sides, and I'd say it's not black or white but depends on how "big" the class is.

The rationale for using getters and setters is so that you abstract out the actual representation as a field, in order to give you the freedom to start presenting this as e.g. a derived value without changing your interface. So really it comes down to how valuable the interface to this class is to you.

If it's part of your first-class public interface, then definitely use getters and setters. At the other extreme, if it's a simple data holder like a tuple that's used solely within a single class (e.g. to map database rows before transformation into another class), then I wouldn't hesitate to use fields; there's no real value to the interface as it's only being used internally.

So how many classes/packages would use this class? If it's a private, "local" class then I don't think there's anything wrong with just using the fields, and updating your callers if this ever needs to change.

Accessing fields is much easier to justify if they're final too, which is often the case with this sort of object.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
1

It's not bad, but usually you'll want to encapsulate the state of an object.

Moonbeam
  • 2,283
  • 1
  • 15
  • 17
0

Standard practice is to make member variables either protected or private with getters/setters that follow java bean convention. This tends to be somewhat verbose, but there is a very nice library (www.projectlombok.org) out there that generates the getters/setters/constructors/toString/hashCode/equals methods for you.

Kevin
  • 24,871
  • 19
  • 102
  • 158
0

It is always a good java programming practice to declare the class variables as private and access them with public getter and setter methods unless its really needed to declare them as public .

If you are using an IDE , then its just a click away to generate getters and setters for class variables/member variables .

Sabya
  • 1,419
  • 1
  • 10
  • 14
0

And now that you have been told over and over to use getter and setters, and because you are in Java (where IDEs help you make getters/setters trivially, and everyone clearly uses them), read over this thread to help add some balance to your usage of them:

Getters and Setters are bad OO design?

Community
  • 1
  • 1
sethcall
  • 2,837
  • 1
  • 19
  • 22