1.1-Introductie-Programmeren

Theory Variables 3

Competence: I understand what the scope of a variabele is.

Specific learning goals:

Introduction

Maybe you’ve found out that it’s sometimes quite challenging where you create variables and from what places in your code you can access them. Sometimes that is challenging! A variabele has a limited scope and the idea is quite simple in the end.

Code blocks

Your code consists of blocks. A block is a piece of code that starts with an opening brace { and ends with a closing brace }. The hard thing of this concept is that you have a lot of blocks inside blocks. Check which blocks are in the following piece of code.

public class Application implements Runnable {

    public static void main(String[] args) {
        SaxionApp.start(new Application(), 1024, 768);
    }

    public void run() {
        int number = 10;
        int number2 = sum(5, 2);

        if (number < number2) {
            SaxionApp.printLine("Number 1 is smaller then number 2");
        } else {
            SaxionApp.printLine("Number 2 is smaller then number 1");
        }
    }

    public int sum(int x, int y) {
        return x + y;
    }
}

Do you see them all? Here they are

  1. A block for the whole class, from the first line till the end.
  2. A block for the content of the main method.
  3. A block for the content of the run method.
  4. A block that is only executed when number1 is smaller then number2.
  5. A block that is only executed when number2 is bigger or equal to number2.
  6. A block for the content of the sum method.

So, your code consists of many blocks and blocks can be nested (So one block can be in another block).

Kortom: Je code bestaat uit heveel blokken en blokken kunnen worden genest (dus een blok kan in een ander blok zitten). When the program is executed each block will be executed from top to bottom.

How does scoping works?

Well, a variable only exists in the block where you have created it. So, when the block ends, the variable is gone too. Of course the variable will only be available in the lines after you have created it. Hard to get? No, I don’t think so. I will explain it based on the code that you’ve seen before.

A good practice is to give variables a scope as small as possible, so you create them in the block where you need them. That is quite efficient for the memory usage of your program, because your program cleans up variables automatically after the closing brace.

Anything more?

Well, there is one thing is that quite helpfull. If you want to create a variable that you can use in your whole program (so in every method), then you can define right after the class opening (on top of your file). This is called a global variable. For example like this:


public class Application implements Runnable {
    ArrayList<String> names = new ArrayList<String>();
    public static void main(String[] args) {
        SaxionApp.start(new Application(), 1024, 768);
    }
    public void run() {
        //You can access names here
        names.add("Mary");
        names.add("John");
        names.add("Rachel");
        String name = getFirst();
    }
    public String getFirst() {
        //And also here
        if (names.size() > 0) {
            return names.get(0);
        }
        return "";
    }
}

Make sure you don’t do this too often. Global variables can be usefull but only do it if it’s really necessary that the variable is available in your whole program, because we want the scoping for variables as small as possible!