Wednesday 4 March 2015

Demystifying static

I had originally started writing a post with the intention of disambiguating a confusion involving static variables, static methods and their behavior in multi-threaded environments in Java. But in the process I realized that the static keyword needs a lot more explanation that I indented to provide. On a hunt to provide a reference to take care of the same, most of the search results which turned up on Google didn't satisfy me. They only succeeded in highlighting the behavior or use of the  static keyword in a very language specific fashion. I wanted to focus on highlighting the meaning and nature of staticHence, a separate post!

Completely understanding static can be tricky as it's language specific definition is often overloaded. But it's purpose in a language remains more or less the same. Understanding this will make most of the semantic rules involving static intuitively derivable. 

DISCLAIMER: This post will attempt to remain language independent and hence all of the below stated characteristics may not be true for the static keyword of all languages.


So, What is static?

 

Binding is the association of an attribute and an entity [1]. For example, when a memory block is associated with an identifier it is called name binding [2]. Another example being type binding - the association of a variable with it's type. A binding can happen at any phase of the execution of a program like compile time or run time.

 static indicates a nature of binding called static binding. When a binding takes place during compile time, it can be referred to as static. Hence, declaring a variable as static can be interpreted as instructing the compiler to perform the bindings associated with the variable during compile time. 

By definition, a static binding cannot be changed during the execution of a program post compile time i.e, in other words, a static binding cannot be changed during run time. Bindings which occur during the latter phase are called dynamic bindings. Dynamic bindings are beyond the scope of this post. 


Static type binding

If the type of a variable is resolved and bound during compile time and cannot be changed post compile time, then the type binding is said to be static.

Static storage binding  

If memory is allocated to a variable during compile time and the variable's identifier remains bound to the allocated memory, then the storage binding is said to be static. [3] The memory cells bound to the identifier do not change during the entire life of the program's execution.


Lifetime of a variable is the time during which a variable is bound to particular address. [6] In simpler words, the lifetime of a variable is the time for which a  variable is alive. Note that this is not to be confused with "scope". Scope of a variable indicates the visibility of a variable. It is possible for a variable to be alive and inaccessible, or in more technical terms - it is possible for a variable to maintain it's state while it's visibility is delimited by it's scope. Eg: private static field variables in Java.

Static lifetime

A static variable is "alive" throughout the entire execution of the program. 


Well, Why static?


Static can be helpful in many ways because of it's early binding time. Early binding implies:
  • High type safety is guaranteed as the type does not change [4]
  • Run time efficiency increases as type resolution is done during compile time [4]
  • High memory safety is guaranteed as memory allocation is done during compile time and is never changed
If you know the basics of compiler and language theory, you would understand the seriousness of type safety and memory safety. And as for memory safety, I think a little programming in C would do it too!


Summary:

Constraints in a language aren't intended to make your life difficult. In fact, they are results of the decisions taken during the design of the programming language to actually make your life better. Ever wondered why a break is mandatory in every case block of the switch-case construct?
 
References:
[1] http://courses.cs.vt.edu/~cs3304/Spring04/notes/Chapter-4/tsld008.htm
[2] http://en.wikipedia.org/wiki/Name_binding
[3]http://groups.engin.umd.umich.edu/CIS/course.des/cis400/maxim/lectures/chp4.htm
[4] http://courses.cs.vt.edu/~cs3304/Spring04/notes/Chapter-4/tsld009.htm
[5] http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.1
[6] http://users.dickinson.edu/~wahlst/356/ch5.pdf

3 comments: