Returning a Value from a Method


As the next step, you want to ask the Student object created in the test for the student's name.

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

You now have two statements in testCreate. Each statement is terminated by a semicolon. When the Java VM executes testCreate, it will execute each statement in turn, top to bottom, before returning control to the calling class.

The second statement is another assignment statement, similar to the first statement. In this statement, however, you are not instantiating a new object. Instead, you are sending a message to the Student object, using the student reference assigned to in the previous statement.

The right-hand side of this statement is a message send that asks the Student object for its name. You, as the programmer and class designer of the Student class, are the one who makes the decision about the message name and its arguments. You have decided that the message to which students should respond is called getName and that the message will not need to carry any additional information (arguments).

 student.getName(); 

You also must specify the message receiverthe object to which you want the message sent. To do so, you first specify the object reference, student, followed by a period (.), followed by the message getName(). The parentheses indicate that no arguments are passed along with the message. For this second statement to work, you will need to define a corresponding method in the Student class called getName().

The left hand of the second statement assigns the memory address of this returned String object to a String local variable called studentName. For this assignment to work, you'll need to define the getName() method in Student so that it returns a String object.

You'll see how to build getName() in just a minute.

Compile all source files. The remaining compilation error indicates that the compiler doesn't know about a getName method in the Student class.

 StudentTest.java:4: cannot find symbol symbol  : method getName() location: class Student       String studentName = student.getName();                       ^ 1 error 

You can eliminate this error by adding a getName method to the Student class definition:

 class Student {    Student(String name) {    }    String getName() {    } } 

You saw earlier how you can indicate that a method returns nothing by using the void keyword. This getName method specifies instead a return type of String. If you now try to compile Student.java, you receive an error:

 Student.java:5: missing return statement    }    ^ 1 error 

Since the method specifies a return type of String, it needs a return statement that provides a String object to be returned to the code that originally sent the getName message:

 class Student {    Student(String name) {    }    String getName() {       return "";    } } 

The return statement here returns an empty String objecta String with nothing in it. Compile the code again; you should receive no compilation errors. You are ready to run the test in JUnit again.



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