Arch1tect
Arch1tect

Reputation: 4281

How to define a name that has boolean value true or false

My program has many functions with a boolean input true or false. And the logic is when it's left, it's true; when it's right, it's false.

So instead of typing

myFuncton(true) //input is left
myFunction(false) //input is right

I want to be able to call myFunction like this:

myFunction(LEFT)
myFunction(RIGHT)

So it's much easier to understand my program

In short, how do I define a name with boolean value in JAVA?

Upvotes: 2

Views: 1761

Answers (4)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

It doesn't seem to me that a boolean is the best fit for your situation, since your goal is not to check for true or false, but rather to see what direction is desired. And if you did use a boolean, which direction should be considered "true"? which false? And what if you instead wanted to add UP and DOWN? Again, a boolean doesn't seem like a great fit.

It looks like your best bet is to instead use an enum:

enum Direction {
  LEFT, RIGHT
}

And then define your method:

public void myFunction(Direction dir) {
  if (dir == Direction.LEFT) {
    // go left
  } else {
    // go right
  }
}

This gives you the advantage of compile time type checking and self-documentation (as per @sqrfv in comments) and so is much better than using a String or a boolean. Also note that enums can have parameters and methods, and thus are much more powerful than String or boolean types.


For example, I wanted to use an enum to associate key strokes with directions for a Swing Key Bindings project, and created an enum actually called Direction like so:

enum Direction {
   UP("Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)),
   DOWN("Down", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)),
   LEFT("Left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)),
   RIGHT("Right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));

   Direction(String text, KeyStroke keyStroke) {
      this.text = text;
      this.keyStroke = keyStroke;
   }
   private String text;
   private KeyStroke keyStroke;

   public String getText() {
      return text;
   }

   public KeyStroke getKeyStroke() {
      return keyStroke;
   }

   @Override
   public String toString() {
      return text;
   }
}

And then used it in my key bindings like so:

class Board2 extends JPanel {
   private static final int PREF_W = 200;
   private static final int PREF_H = 400;
   public static final String DIRECTION = "direction";
   private Direction direction = null;

   public Board2() {
      setBorder(BorderFactory.createTitledBorder("Board2"));
      InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
      ActionMap actionMap = getActionMap();

      for (Direction dir : Direction.values()) {
         inputMap.put(dir.getKeyStroke(), dir.getText());
         actionMap.put(dir.getText(), new MyArrowBinding(dir));
      }
   }

   private class MyArrowBinding extends AbstractAction {
      private Direction dir;

      public MyArrowBinding(Direction dir) {
         super(dir.getText());
         this.dir = dir;
         putValue(ACTION_COMMAND_KEY, dir);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         setDirection(dir);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   public void setDirection(Direction direction) {
      Direction oldValue = this.direction;
      Direction newValue = direction;
      this.direction = newValue;

      firePropertyChange(DIRECTION, oldValue, newValue);
   }

   public Direction getDirection() {
      return direction;
   }
}

Upvotes: 12

Steven Rogers
Steven Rogers

Reputation: 2004

Final int LEFT = 0
Final int RIGHT = 1

That's how I did my keystrokes, except there was forward and backwards included.

Upvotes: 0

user2932397
user2932397

Reputation: 184

Look in to defining constants. You'll want them public/protected/private depending on your use.

static final boolean LEFT = true;

static final boolean RIGHT = false;

Edit: made RIGHT false.

Upvotes: 2

Ankit Rustagi
Ankit Rustagi

Reputation: 5637

Use final variables, final makes sure you cant change the variable's values.

final boolean LEFT = true; 
final boolean RIGHT = false;

then you can use them like this

myFunction(LEFT)
myFunction(RIGHT)

Upvotes: 10

Related Questions