How to Make a Private Boolean in Java

How to Make a Private Boolean in Java thumbnail
In programming, booleans have a value of either true or false.

Java programs store data in variables. When declaring a variable, programmers specify the type of the data it will store, the name of the variable and its initial value. Programmers also optionally indicate access to a variable within the application by using a visibility modifier. If a variable is declared as private, it can only be directly accessed from within its own class.

Instructions

    • 1

      Choose a name for your new boolean variable. Choosing a name that is meaningful will make your program easier to read and therefore work with, so choose something that suits the purpose of the variable value, or its role within your application. When you declare your boolean variable, observe the conventions for naming in Java, through which you start each section of the name with an uppercase letter, as follows:
      isTooLong

      For boolean variables, it is common to use this type of name, since the content of the variable will reflect either a true or false value.

    • 2

      Declare your new boolean variable within your program. If you are using the boolean as a class or instance variable, declare it near the top of the class declaration, before the class constructor method. This allows you to set the value of the variable, or access its value, anywhere else in the class. The following sample Java code demonstrates declaring a new boolean variable:
      boolean isTooLong;

      This code is all you need to declare a boolean variable, but you can opt to carry out additional processing and to specify the variable's visibility.

    • 3

      Declare your new boolean variable as having private visibility. The following extended code applies this process:
      private boolean isTooLong;

      Whatever class this line of code appears in is the only class the variable will be accessible within. If you do not declare a visibility modifier for your variable, it will be accessible throughout the application package by default. (See References 4)

    • 4

      Assign a value to your variable. If the new boolean is an instance variable, you can assign its value within the constructor method as follows:
      isTooLong = false;

      Alternatively, assign the value to your variable when you declare it, initializing it within a single line of code as follows:
      private boolean isTooLong = false;

      You can alter the value of the variable at any later point within your class, setting its value to suit the logic of your application.

    • 5

      Provide access to your boolean variable. Depending on the purpose of the variable, you may wish to provide a method granting access to it, for external application code. The following sample method demonstrates this technique:
      public boolean isItTooLong() { return isTooLong; }

      This allows "customer" code to access the value of the variable. In some cases you may not need to grant this access -- for example, if the variable is purely for internal class use.

Tips & Warnings

  • You can also use the boolean wrapper class if you need to store your boolean as an object instance.

  • Be careful if you decide to allow external code to set the value of your variable, particularly if it is private.

Related Searches:

References

Resources

  • Photo Credit Hemera Technologies/Photos.com/Getty Images

Comments

Related Ads

Featured