Skip to content

Latest commit

 

History

History
61 lines (43 loc) · 3.5 KB

1.md

File metadata and controls

61 lines (43 loc) · 3.5 KB

What are Constants?

Constants, as common sense dictates, are things which do not change their value. Pi is a constant, or rather, a very well known constant because no matter who you ask, you will get the same value of Pi. On the other hand, the weather in your city is a variable: it changes its value every time you check it.

Constants in programming

Almost every programming language (as far as I know of, sans JavaScript & Python) have constants in some form or fashion:

  • Pascal's const block.
  • QBASIC had CONST. E.g. CONST PI = 3.1415
  • In C, we use #define. E.g. #define PI 3.1415.
  • In C++, we have const. E.g. const int PI = 3.1415
  • etc.

In Java, simply put the keyword final as the start and boom you have a constant. For example, final double PI = 3.1415. One important point I want you to remember is that a constant in Java can be declared in two places:

  1. At the same it is declared, the same way, say as in C, or,
  2. In the Constructor. This allows programmers to pass in values to the constructor which can then be used to initialize constants when the object is being created.

What's the big deal?

The biggest issue I notice when reading or reviewing code is that the variables and constants are declared in identical ways and no distinction is made. If you are doing this, stop doing it right now. This makes your class hard to read, maintain and may carry performance penalties (Although, with the new, smarter HotSpot, I doubt it). Let's look at an example:

class Person
{
    String name; // -- never changes value - should be a constant
    Date birthDate; // -- also could never be changed
    long phone; // -- OK - This can be changed and is correctly declared as variable

    public Person(String name, int age, long phone) {this.name = name; ....}

    public setPhone(long phone) { 
        this.phone = phone;
    }
 }

In the above example, all three member variables are declared identically (i.e. TYPE VARIABLE_NAME). However, there is a subtle difference: The variables name and birthDate can only be set via the constructor and cannot be changed once the class is created. That is, effectively, the two 'variables' are actually constants. Java itself doesn't force programmers to use an alternate syntax and issues no warnings when it detects this phenomenon. I can think of a few reasons why: Inheritance and Reflection comes to my mind, but anyways, I'm getting ahead of myself so without further ado, let me introduce the first golden rule:

Rule #1:

Do not leave Puedo constants in your code. Mark them explicitly with final.

The above example is fixed below:

class Person
{
     final String name; // -- `final` indicates constant. Thumbs up.
     final int age;
     long phone; // -- Not a constant. Hence no final

     public Person(String name, int age, long phone) { this.name = name; ....}

     public setPhone(long phone) { 
        this.phone = phone;
     }
}

You can also use upper case in constant names, although I prefer doing that only if you are assigning value to the constant at the same time you declare it.

e.g.

 final double AVOGADRO_CONSTANT = 6.023e23

What are the benefits?

Constants or Immutable variables have many benefits, thread-safety and code readability being the main ones, as we shall see in the next principal.