Competence: I understand what the scope of a variabele is.
Specific learning goals:
{}
.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.
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
main
method.run
method.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.
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.
int number
is available in the whole run method. So from the point you’ve created it until the end brace of the main method. It will even be available inside the if blocks.int number2
is also available in the whole run method. The only thing is that you can’t use it in the line above the line where we initialize it, because it isn’t created yet ;-).sum
method, so the x and y, are only available in that method. Besides that they are not available. Something you can use outside this sum method is the return value that it gives to the method who did the call to this method.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.
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!