Welcome to TheUrbanPenguin and the series of free tutorials we have running to prepare you for the OCA exam 1ZO-803, the Java 7 SE Programmer 1 certification from Oracle. Each tutorial will step you through a specific objective of one of the 8 topics from the exam. Today we look at what we mean by variable scope . Each variable has a scope in which it is effective. That is usually the block of code in which it is defined. As a variable falls out of scope then that is the signal to the garbage collector that the memory it used can be released back to the system.
We start with a simple class definition for our example:
package tup; public class VariableScope { public static void main(String[] args) { } }
Nothing much to see here folks, just the package, class and in the class the main method which is currently empty. Note that class name are usually written with an uppercase character for the start of each word and no spaces between the words, the rest in lowercase: VariableScope .
We now will add a variable to the the class. Variables defined int he class are often called fields and can be instance or class fields. First we add an instance field:
package tup; private int accountNumber; public class VariableScope { public static void main(String[] args) { } }
The variable name is accountNumber and it is normal with a variable to start the first word with a lowercase letter. It is known as an instance variable or field as a new instance of the field is created with each instance of the object that is created. So if I create fice instances of the class VariableScope I will have 5 separate accountNumbers, this seems logical for the application. Now we will add in a variable using the static modifier. This will make the variable a class variable so each instance of the class shares the single variable:
package tup; private int accountNumber; static int nextAccountNo = 100; public class VariableScope { public static void main(String[] args) { } }
We have also initialized the class variable nextAccountNo to a valuer of 100. When setting the accountNumber we will increment the nextAccountNo. In this way we have a mechanism to generate new account numbers.
Both the class and the Instance variables are available to all items within the class. So we can set and read the accountNumber with separate methods. First we look at the getter used to read the value from the accountNumber field.
package tup; private int accountNumber; static int nextAccountNo = 100; public class VariableScope { public static void main(String[] args) { } public int getAccountNumber() { return accountNumber; } }
See that we can access the variable accountNumber from the getAccountNumber method. this is because the variable accountNumber was declared in the Class. We we look at the setter for the accountNumber field we will use a local variable. Local variables are defined within a method or other code block and only available to the method or code block.
package tup; private int accountNumber; static int nextAccountNo = 100; public class VariableScope { public static void main(String[] args) { }
package tup; private int accountNumber; static int nextAccountNo = 100; public class VariableScope { public static void main(String[] args) { } public int getAccountNumber() { return accountNumber; } public void setAccountNumber() { int accountNumber = nextAccountNo; nextAccountNo++; this.accountNumber = accountNumber; } } }
We declare and initiate the local variable, accountNumber from the class variable nextAccountNo. We then increment the nextAccountNo and populate the instance variable AccountNumber. To ensure Java is certain which variable is which we can refer to the Instance variable using thethis keyword. meaning this object. Even though we can use the same name for the local and instance variable it is not necessarily a good idea as you can see it may make for confusion.
In our main method now we can create two instances of our VariableScope object to see our code working.
package tup; private int accountNumber; static int nextAccountNo = 100; public class VariableScope { public static void main(String[] args) { VariableScope VS1 = new VariableScope(); VS1.setAccountNumber(); System.out.println(VS1.getAccountNumber()); VariableScope VS2 = new VariableScope(); VS2.setAccountNumber(); System.out.println(VS2.getAccountNumber()); } public int getAccountNumber() { return accountNumber; } public void setAccountNumber() { int accountNumber = nextAccountNo; nextAccountNo++; this.accountNumber = accountNumber; } } }
We should see the account numbers 100 and 101 being printed to show we started on the nextAccountNo and then incremented it.
Local variables can also we seen at play in looping structures:
for(int x = 1 ; x < 10; x ++){ System.out.println(x); }
The local variable x is defined in the for loop, int x, the variable then is available only within the loop and not accessible outside. If we need to access it outside the loop then we declare the variable outside the loop.
int x; for(x = 1 ; x < 10; x ++){ System.out.println(x); }
Now that we have defined the variable outside of the loop we can access the variable both within and without the loop.