Local Variables


So far, when JUnit executes testCreate, Java executes the single statement that constructs a new Student object. Once this statement executes, control returns to the calling code in the JUnit framework. At this point, the object created in testCreate disappears: It had a lifetime only as long as testCreate was executing. Another way to say this is that the Student object is scoped to the testCreate method.

You will want to be able to refer to this Student object later in your test. You must somehow hold on to the memory address of where the Java VM decided to store the Student object. The new operator returns a reference to the object's location in memory. You can store this reference by using the assignment operator, represented in Java as the equals sign (=). Modify StudentTest:

 public class StudentTest extends junit.framework.TestCase {    public void testCreate() {       Student student = new Student("Jane Doe");    } } 

The statement is now an assignment statement: an object or value on the right-hand side of an assignment operator is stored in a reference on the left-hand side of the operator.

When the Java VM executes this statement, it executes the code to the right-hand side of the assignment operator (=) first, creating a Student object in memory. The VM takes note of the actual memory address where it places the new Student object. Subsequently, the VM assigns this address to a reference on the left-hand side, or first half, of the statement.

The first half of the statement creates a local variable reference named student of the type Student. The reference will contain the memory address of the Student object. It is local because it will exist only for the duration of the test method. Local variables are also known as temp variables or temporary variables.

You could have named the variable someStudent, or janeDoe, but in this case, the generic name student will work just fine. See the section Naming Conventions at the end of this lesson for guidelines on how to name your variables.

A conceptual pictorial representation of the student reference and Student object might look something like Figure 1.5.[7] This picture exists only to give you an understanding of what's happening behind the scenes. You need not learn how to create your own such pictures.

[7] The memory addresses are shown in hexadecimal, which is indicated by the "0x" that precedes each number. Lesson 10 includes a discussion of hexadecimal numbers.

Figure 1.5. Reference to an Object


Behind the scenes, Java maintains a list of all the variables you define and the memory location to which each variable refers. One of the beauties of Java is that you do not have to explicitly code the mundane details of creating and releasing this memory space. Developers using an older language such as C or C++ expend a considerable amount of effort managing memory.

In Java, you don't have to worry as much about managing memory. But it is still possible to create an application with memory "leaks," where the application continues to use more and more memory until there is none left. It is also possible to create applications that behave incorrectly because of a poor understanding of how Java manages memory for you. You must still understand what's going on behind the scenes in order to master the language.

I will use the conceptual memory diagrams only in this chapter to help you understand what Java does when you manipulate objects. They are not a standard diagramming tool. Once you have a fundamental understanding of memory allocation, you can for the most part put the nuts and bolts of it out of mind when coding in Java.

Recompile all source files and rerun your test. So far, you haven't written any code that explicitly tests anything. Your tests should continue to pass.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net