Non Access Modifiers in Java I: static and strictfp

Non access modifiers qualify a method or variable with additional functionality. In one of the previous lessons, we saw that access modifiers affect the visibility. Non access modifiers are directions to Java runtime that the variable or method qualified with the modifier requires special treatment. The special treatment offered depends upon the modifier specified.

Types of Non Access Modifiers

Java supports the following eight non access modifiers

  1. static
  2. strictfp
  3. final
  4. synchronized
  5. transient
  6. volatile
  7. abstract
  8. native

It’s not possible to cover all of these in one article, so these modifiers are split between this article and next. This article covers static and strictfp modifiers.

Other prerequisite – You need at least elementary understanding of what happens Inside JVM.

The static Modifier

The static modifier can qualify

  1. Variables
  2. Methods
  3. Code Blocks
  4. Classes

We discuss all of these one by one. Remember, that anything static exists in a universe of its own cut-off from the world of objects and instances and references. This means that static elements cannot access non-static elements.

static Variables

  • Only class level variables can be static, local variables – e.g. those declared inside a method or constructor cannot be static.
  • static variable means that there is at the most one instance of this variable
  • A static variable is also called class level variable. It is associated on a per-class basis. All instances of the same class share the static variable.
  • Can be initialized at declaration time
  • Can be used for inter-communicating among objects of same class. For example, you can have a non public static variable which allows you to share information among all objects of same class.
  • If also declared final, a static variable can be used as a constant. By convention, constants are declared as having all capital letters, For example, you might come across the following typical Java snippet –
1234567// declare small g = 9.8 // acceleration due to gravity // it is small, but is declared with all caps public static final float SMALLG = 9.8;

static Methods

  1. Like static variables, are associated with class and not an object.
  2. Are accessed using the syntax ClassName.staticMethodName()
  3. Exist indpenendent of any object
  4. Don’t have access to the reference of current object – this
  5. Can access other static methods and variables
  6. You can create new objects in static methods
  7. Are executed before any non-static code is executed.
  8. A prime example of static methods would be the main method. The main method is called before any instance of the class in which main method is defined exists. Just for refresher, here is the signature –
1public static void main(String... args) {

static Blocks

We already learned about code blocks. Turns out that you can start a code block with the word static and of course the ‘{‘.

Every static code block is guaranteed to be run before the main method is called. So, you can use static blocks for executing code before your application starts.

For example, if you are about to start an application which would at some point instantiate Politicians to robPeople(), you would notify the FBI and Interpol in the static block to give them advance warning.

1234567891011121314151617static { alertFBI("ALERT: Politicians are coming"); alertInterpol("ALERT: Politicians are coming"); } // somewhere down below in execution // start the PoliticiansVSPeople application // this application, during the course of its execution // produces the creepy crawlies PoliticiansVSPeople creepyCrawlies = new PoliticiansVSPeople ();

But, let’s say you want to write a Chemistry application, where you would have access to all the elements (Hydrogen, Helium etc.). Since you just want an array or collection to be populated  before application starts, it can be done with static variables. This situation does not warrant a static code block.

static Classes

  1. A class declaration can be qualified with static variable
  2. A static class can only be declared inside another class. A static class cannot stand on its own.
  3. A static class cannot access non-static objects.
  4. A static inner class acts as a completely independent class.
  5. When an object of the outer class is created, no instance of inner class is automatically created.
  6. A static inner class shines in (the very rare) case where the non-public static members need to be accessed by another class.

Inner classes and static inner classes are further discussed in object oriented programming tutorial.

The strictfp Modifier

  1. strictfp ensures portability of floating point calculations
  2. Using strtictfp may bring down the precision of your float calculation
  3. Is applicable to code and not variables
  4. Only the following constructs can have strictfp qualifier
    1. Classes
    2. Methods
    3. Interfaces
    4. Code Blocks
  5. In a non strictfp float arithmetic, the intermediate steps in a floating pointing calculation can generate results that are not in the standard 32 and 64 bit single and double precision format.
  6. By default JVM implementation uses intermediate formats to take maximum advantage of the underlying hardware’s capabilities. This ensures that round off and overflow errors are minimum.
  7. Since, by default, JVM takes advantage of hardware to possibly have different results on different hardware, you may end up with different final results on different platforms.
  8. The above default behavior is in general a welcome thing. Sometimes, if you must disable (and in real life you seldomly do) this behavior, you can do so by using strictfp modifier.
  9. Remember, it does not make any sense (and is not allowed either) to qualify variables, constructors and abstract methods with strictfp.

Leave a comment

Your email address will not be published. Required fields are marked *