# Symbolic Constants

• What is a symbolic constant ?

• A symbolic constant is an "variable" whose value does not change during the entire lifetime of the program...

• Some constants in Mathematics:

Name of the constant Value of the constant
Pi 3.1415926535...
e (Natural log) 2.718281828...

• Some constants in Phisics:

Name of the constant Value of the constant
Gravitational Constant 6.67300 × 10-11 (m3 kg-1 s-2)
c (speed of light) 299,792,458 (m/sec)

• Why use symbolic constants ?

• It is generally a bad idea to write a constant out in a computer program each time that the constant is used:

• It is prone to error
• It is not convenient

• Consider the following program that computes the area of 2 circles:

 ```public class Circle1 { public static void main(String[] args) { double r1 = 3.4; // Radious of first cicle double r2 = 5.4; // Radious of second cicle double area1; // Area of circle 1 double area2; // Area of circle 1 area1 = 3.1415926535 * r1 * r1; area2 = 3.1415926535 * r2 * r2; } } ```

• You can easily mistype a digit of Pi...
• You have to remember the digits of Pi...

• So it is more convenient to give a name to a constant.

This name assigned to a constant is then called a symbolic constant

• Defining symbolic constants in Java

• Well, just like everything that we have seen so far, Java is a language.

So you can guess what's coming...

We will learn a new syntax construct to express the fact that we want to define a symbolic constant ,

IMPORTANT: Syntax Rule

• Syntax to define a constant:

A symbolic constant can be defined:

1. inside a method - such a constant can only be used by the method in which it is defined, or
2. inside a class (outside all methods) - the access of such a constant is determined by the given access specifier

Syntax to define a constant inside a method:

 ``` final typeName variableName = expression; ```

Syntax to define a constant inside a class (outside methods):

 ``` accessSpecifier static final typeName variableName = expression; ```

• The use of the keyword final means:

the value is "finalized" (once a value is given to the variable, the value cannot be changed further)

• The use of the keyword static means that:
the variable belongs to the class and the variable exists from the start of the program

(This will be explained in greater details in Chapter 9)

• accessSpecifier can be either public or private

Private constants (and variables) can only be accessed from within the class - we have see this in: click here

• Example how to define a constant:

 ``` public class MyClassName { public void Method1() { final double e = 2.718281828; // Constant in method } public void Method2() { final int c = 299792458; // Constant in method } public static final double Pi = 3.1415926535; // Constant in class } ```

NOTE: I have LEFT OUT all statements and other things from the class and method to HIGH LIGHT the SYNTAX on how to define constants.

Do not try to read anything into the methods.

(You need to know how to use it before using it in a program....)

• What's the difference between a constant and a variable

• When you use the keyword final, it tells the Java compiler that the value of that variable CANNOT be changed ,

• In other words, when you write
```	final double Pi = 3.1415926535;
```
Java will forbid you to change the value of Pi, e.g., this is forbidden:
```        Pi = 1.0;
```

• DEMO Program: (Demo above code)

Presentation:

• Compile - no error
• Remove commented assignment "e = 1.0;" and compile - ERROR (because we try to change the value of a constant)

• A complete example that uses constants

• Consider the BankAccount class that we have written.

• For sake of example, let's assume that the interest rate earned by every bank account is always equal to 4%

(OK, this is only an artificial situation. I am creating this situation to introduce the use of constants. I could use other more meaningful examples, but then I would have to explain a new computer program - since you have seen the BankAccount class, I don't need to explain the programs to you... So for dictatical reasons, I am using BankAccount as the base)

• Here is the original BankAccount class:

 ``` public class BankAccount { // Constructors public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; } // Methods public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { balance = balance - amount; } public double getBalance() { return(balance); } // Instance field(s) private double balance; } ```

• Here, we add a constant named interestRate to the BankAccount class:

 ``` public class BankAccount { // Constructors public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; } // Methods public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { balance = balance - amount; } public double getBalance() { return(balance); } // Instance field(s) private double balance; private static final double interestRate = 4.0; // 4% interest } ```

• Next, we add an addInterest() to the BankAccount class that adds the interest at the given rate to the balance...

Notice that this addInterest() method does not need any parameters because the interest rate is given in the constant interestRate (it has sufficient information to do its job)

 ``` public class BankAccount { // Constructors public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; } // Methods public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { balance = balance - amount; } public double getBalance() { return(balance); } public void addInterest() { balance = balance + (interestRate/100) * balance; } // Instance field(s) private double balance; private static final double interestRate = 4.0; // 4% interest } ```

• DEMO Program: (Demo above code)

Presentation:

• Open BlueJ
• Set breakpoint at statement "BankAccount harrysChecking = new BankAccount(1000);"
• Run main()
• Inspect the BankAccount CLASS...

You should see that the constant value 4 is defined inside the BankAccount CLASS...

• Run the program; it creates 2 BankAccount objects, each with a "balance" variable.

• Naming convention for Symbolic Constant

• Many programmers like to name the symbolic constants with all caps.

• For example:

 ``` public static final double PI = 3.1415926535; public static final int SPEED_OF_LIGHT = 299792458; ```

• Packaging Constants

• If your program uses many constants, then it would be a good idea to put all the constants in one place.

• In Java, a class is uses to hold:

• Constructors
• Methods
• Variables
• Yes, also constants !!!

• You may want write a class that only holds constants.

Example:

 ``` public class MyConstants { public static final double PI = 3.1415926535; public static final double E = 2.718281828; } ```

• When you write another program class that uses the constants in the MyConstants class, you can refer the constants as follows:

 ``` ClassName.ConstantName ```

• Use as ClassName the name of the class that contains the constant

In the above example, the name of the class is MyConstants

• Use as ConstantName the name that you (or someone else) have given to the constant

In the above example, the name of the 2 constants are PI and E

• Here is an example program that uses the constant PI in the MyConstants class:

 ``` public class UseConstants { public static void main(String[] args) { double r = 10; double area; area = MyConstants.PI * r * r; // ^^^^^^^^^^^^^^^ System.out.print("radius = "); System.out.println(r); System.out.print("area = "); System.out.println(area); } } ```

• DEMO Program: (Demo above code)

• Java's Math class

• Java has provided (i.e., packaged) the above constants in their API.

• The class that contains these Mathematical constants is called (for obvious reasons): Math

• Here is the API documentation for the Math class: click here

• We can rewrite the above program using the constants in Java's API as follows:

 ``` public class UseConstants { public static void main(String[] args) { double r = 10; double area; area = Math.PI * r * r; System.out.print("radius = "); System.out.println(r); System.out.print("area = "); System.out.println(area); } } ```

• All the Constants that are defined in Java

• Java has a large number of constants defined and stored in difference classes.

• For a full list of constants and the classes that contain them, see: click here