Javac · Java Tutorials - Final Primitive Data Type Variables Tutorial

Final Primitive Data Type Variables Tutorial

The keyword final can be applied to many things, in the next few tutorials I will explore how it affects the behavior of methods, classes, and variables. When the keyword final is applied to primitive type variables, it will prevent a primitive type variable from changing its value once it has been initialized. A final primitive type variable can only be initialized or assigned a value once; once it is assigned a value, it cannot be changed ever again. The value does not need to be assigned when the final primitive type variable is declared; this kind of a final primitive type variable is called a blank final variable. A blank final instance variable must be assigned a value inside of a constructor or a compiler error will occur. A blank final local variable must be assigned a value before it is used. The optional common naming convention for final primitive type variables is to be in uppercase with an underscore (_) separating the words. I am already splitting up the final primitive type variables and final reference variables into separate tutorials, so I was debating of splitting this tutorial into a part 1 and 2. I decided against that and I just want to let you know that this tutorial will throw a lot your way.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md FinalPrimitives
C:\Java>cd FinalPrimitives
C:\Java\FinalPrimitives>Notepad FinalPrimitives.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class FinalPrimitives {
    public static void main(String args[]) {
        FinalInstanceTester fit = new FinalInstanceTester((byte) 127);
        fit.displayValues();

        System.out.println("-------");
        short length = 4;
        final short height = 4;
        FinalLocalTester flt = new FinalLocalTester();
        System.out.println("Volume = " + flt.calculateVolume(length, height, 4));
    }
}

class FinalInstanceTester {
    final byte BYTE_TEST; // blank final instance variable
    final int INT_TEST = 45; // final instance variable
    final boolean TRUE_VALUE = true; // final instance variable
    final char UPPER_A = 'A'; // final instance variable

    FinalInstanceTester() {
        super();
        BYTE_TEST = 0; // must assign a value to a blank final instance variable in a constructor
    }

    FinalInstanceTester(byte paramValue) {
        BYTE_TEST = paramValue; // must assign a value to a blank final instance variable in a constructor
    }
	
    void displayValues() {
        //BYTE_TEST = 127; // error, must be initialized in a constructor
        //INT_TEST++; // error, cannot change the value of a variable marked final once it has been initialized.
        System.out.println(BYTE_TEST); // error, if not initialized in a constructor.
        System.out.println(INT_TEST);
        System.out.println(TRUE_VALUE);
    }	
}

class FinalLocalTester {

    // Method parameter variables are basically implemented as local variables. 
    // They have the same visibility (not outside of the method) and lifetime (created on invoking a method, destroyed on method return). 
    // Technically speaking, Oracle splits parameter and local variables into separate categories, but to my knowledge, there is no difference.
    int calculateVolume(final short length, short height, int width) {
        //length++; // error, cannot change the value of a variable marked final once it has been initialized.
        height++;
        width++; 
		
        final int enlarger; // blank final local variable
        enlarger = 2;
        //enlarger++; // error, cannot change the value of a variable marked final once it has been initialized.
        final int shrinker = 2; // final local variable
        //shrinker--; // error, cannot change the value of a variable marked final once it has been initialized.
        return (length * height * width * enlarger / shrinker);		
    }
}


Now switch back to the command prompt (CMD) and type in javac FinalPrimitives.java and press Enter.
Now type in java FinalPrimitives and press Enter.


C:\Java\FinalPrimitives>javac FinalPrimitives.java
127
45
true
-------
Volume = 100


Final thoughts

The most important thing to learn from this tutorial is that once a final primitive type variable is initialized, its value can never be changed. I know this tutorial most likely caused some level of confusion and I was debating of splitting this tutorial into a part 1 and 2. I figured that if I did so you would be watching two tutorials anyway. If you got everything, great, if not, then feel free to watch it again.


Tutorials