Home > IB Computer Science 2016 > Meaning of the word “static” in computer science

Meaning of the word “static” in computer science

September 23, 2014 Leave a comment Go to comments

There has been some confusion about this. Let’s clear it up.

Static data structures

An array is a “static data structure” because its size is set at the moment it is created and it cannot ever change. If you want to re-size an array, you have to make a whole new one and copy the contents of the old one into the new one. This can be an expensive operation and it will cause any objects that hold a reference to the original array to lose it entirely. You also can’t insert values easily at the beginning of a populated array. It’s a bit like arriving late at the theatre and trying to sit in an aisle seat. If the seat is occupied, you can’t just “insert” another seat. Everyone would have to get up and move along once seat to create a space for you.

The opposite of static data structure is “dynamic data structure”. An example of a dynamic data structure is a linked list. Since a linked list uses object references to give the illusion that list items are next to each other, it is easy to add items to the end of the list and even insert them in the middle. There aren’t any good real-world examples of linked lists. Trains and daisy-chains go some way to explaining how they work, but they miss the crucial point that items that are logically next to each other in the list are not physically next to each other in memory.

Now, because static has another meaning, when you are talking about an array being a static data structure I want you to say just that, “an array is a static data structure”. Do not say, an array “is static”, because that could be ambiguous, as you will see.

The static keyword in Java

Remember the difference between a class and an object? “Student” is a class from which we can create as many objects as we like. So “Akhi Amanullah” is an object of type Student. As is “Ethan Fleming” and “Jun Park”. Normally, when we give a class attributes (fields, variables, same thing…), they each get their own copy of that attribute. Hence the student objects mentioned above have their own names, their own heights, their own subjects, etc. However, when we declare a variable or method as static, there is only one of them for the whole class, not one for each object created from the class. That means if a static variable gets a new value, then all instances of the class see the new value. Indeed, there doesn’t even have to be an instance for you to use a static variable. That’s because it’s a class variable, not an object variable. Static variables are often used to store any information that is true for all objects of a class, or which never change, like constants. They can be accessed by using dot notation with the name of the class, e.g. Math.PI is Java’s value for pi. Math is a class and I didn’t have to instantiate it before using its PI variable. That’s because the PI variable is declared static.

The problem that student programmers have with static arises from the fact that the entry point of a program in Java is a main method that is declared static. Of course, there has to be at least one method that is declared static because otherwise you would have to instantiate an object before you could ever even run any line of code, and since instantiating an object IS running a line of code, you’d be stuck! So immediately that you start running your program, you are in what Java calls a “static context”. If you now try to access a variable or run a method that is not declared static, you will have a problem, as shown here:

class Dog{
   public static void main(String[] args){
      woof(); // PROBLEM
   }
   void woof(){
      System.out.println("Woof");  
   }
}

The error you get here is “non-static method woof() cannot be referenced from a static context”. We have tried to call the woof() method without having created any Dog objects. So I’ll create some Dog objects, right?

class Dog{
   public static void main(String[] args){
      Dog a = new Dog();
      Dog b = new Dog();
      Dog c = new Dog();
      woof(); // STILL A PROBLEM
   }
   void woof(){
      System.out.println("Woof");  
   }
}

You get exactly the same error here: “non-static method woof() cannot be referenced from a static context”. The problem now is that we haven’t specified which dog’s woof() method we should call. Here is some code that works:

class Dog{
   public static void main(String[] args){
      Dog a = new Dog();
      Dog b = new Dog();
      Dog c = new Dog();
      a.woof(); // WORKS!
   }
   void woof(){
      System.out.println("Woof");  
   }
}

This works because having called a.woof(), we are no longer in a “static context”. That means we are calling a method that belongs to an object, instead of trying to call a method that belongs to the whole class.

Another strategy to get round this problem is to always separate the class that contains the main() method from the classes that model the objects in your program.

public class Main{
   public static void main(String[] args){
      Game theGame = new Game();
      theGame.play();
   }
}

class Game{
   void play(){
      // etc 
   }
}

Notice how we have left the static context immediately, so we can’t have any problems later.

Finally, a lazy way of getting round problems associated with static is to declare EVERYTHING static. If you find yourself doing this, then either (a) you are being lazy or (b) you shouldn’t be using an object-oriented language at all.

Advertisements
  1. January 31, 2017 at 6:00 am

    Looks fine to me. Perhaps you need to scroll right a bit? 😁

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s