This chapter only covers the ways data is stored for the exclusive use of a program while the program is running. In particular, storage in files on the disk or other media is not discussed here. Technically speaking, this chapter is about storage of data within a single process.
Background: A single run of a program is called a process. The program is the software that might be bought, downloaded, copied, or compiled from source code. Programming courses, of course, focus on programs that you create yourself in source form, which you then compile and run. The program itself is a static entity: It has no behavior unless it is loaded into a computer and run. Please recognize that the same single, static program may be run many different times, within the same computer or within different computers. A particular run of a program is a dynamic entity. A dynamic entity is anything that changes as time passes; dynamic entities have history. So, the program itself must be called something different from what we call a particular run of it so we don't get confused. Computer scientists reserve the word "program" for the static entity and the word "process" for a single dynamic entity of one run of one program.
There are just a few different kinds of storage in memory. Unfortunately, none of their distinctions is apparent to people when they use the computer, so programmers and other computing professionals must have paid special attention in order to learn something new because they must work with these distinctions. In this chapter we will cover only the first two: Local variables and dynamic objects. The good news is that their is just one more: static variables. But local and dynamic are new enough for now so we will make it easier by leaving statics for later.
The first kind is familiar from the first examples of programs that you have seen. They belong to methods.
class LocalDemo
{
public static void main(String[] args)
{
int sum = 0;
int limit = 20;
for(int i = 0; i < limit; i++)
{
sum = sum + i;
}
System.out.println("The sum of the integers from 0 to "
+ limit
+ " is "
+ sum);
}
}
It is important to recognize that every variable declared anywhere inside a method body is a local variable. The above example shows three local variables (named sum, limit and i) declared within a static method. We will see local variables within instance methods below. We will also see later local variables whose type is the name of a class, declared like "Picture pict;" This is deferred because objects like instances of the Picture class are going to be explained first.
Common Misunderstanding: Declarations like "private int sum;", "public int sum=3;", or "Picture target;" coded directly within the {...}s of class definitions are NOT local variables! They are called declarations of instance variables, also called fields. This will be further explained below.
It will be easier to understand these new concepts about Java when you recognize a fairly obvious fact about computer memory. A run of a program (briefly, a process) "uses" memory. How often do you seen a program fail and give a message about "insufficient memory"? You must have seen advertisements for "DRAM" you can buy to add more memory to your computer; many of you actually replaced smaller memory chips with larger ones to "upgrade your computer's memory". Memory is implemented with real, physical hardware that costs money to manufacture and buy. The more memory you want, the more it costs.
The obvious fact is that when a process uses memory, the memory is NOT "used up". Even if the process fails, whatever memory it began using becomes available for use by new processes when the process finishes. It is more accurate to say that a running program "borrows" memory for its exclusive use than to say that it "uses" memory. At some point in time, the borrowed memory is given back and becomes available for new uses. In fact, this borrowing and returning occurs many different times during the period when single process runs, not just when the process begins and ends.
So, the key feature of local variables can now be easily explained:
The memory hardware used for a local variable is borrowed at the time when the method containing the variable is called and that same memory hardware is given back for other uses at the time when the corresponding call returns.
Of course, the call returns when the method code execution reaches a return statement or the end of the method body. (Remember that methods that return values must always use the return statement to return, while methods declared "void" can return by executing a "return ;" statement or by reaching the end of their bodies.)
The second kind of storage can be called dynamic objects. A dynamic object is allocated when the process executes a Java "new" operation. The chief effect of the new operation is to allocate a dynamic object. The new operation must be applied to a class name followed by parentheses enclosing zero or more arguments.
Example: This examples illustrate a class for dynamic objects that can be used for counting together with a class for a program to demonstrate and test them.
class Counter
{
private int count;
public Counter()
{
this.count = 0;
}
public void addOne()
{
this.count = this.count + 1;
}
public int get()
{
return this.count;
}
public void print()
{
System.out.println("Counter's count is now "
+ this.get());
}
}
class CounterDemo
{
public static void main(String[] args)
{
Counter aCounter;
Counter anotherCounter;
aCounter = new Counter();
anotherCounter = new Counter();
for(int i=0; i < 10; i++)
{
aCounter.addOne();
anotherCounter.addOne();
anotherCounter.addOne();
}
aCounter.print();
anotherCounter.print();
}
}
There has been error in communication with booki server. Not sure right now where is the problem.
You should refresh this page.