Creating a Student


Add a single line, or statement, to the testCreate method:

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

You terminate each statement with a semicolon (;).

When the testing framework calls the testCreate method, Java executes this single statement. Once Java executes the statement, it returns control to code in the testing framework that called testCreate.

The statement in testCreate tells Java to create an object of the class type Student:

 new Student("Jane Doe"); 

You place the new keyword before the name of the class to instantiate. You then follow the class name with an argument list. The argument list contains information the Student class requires in order to be able to instantiate a Student object. Different classes will need different pieces of information; some classes will not need any information at all. It is up to the designer of the class (you, in this case) to specify what information must be supplied.

The sole argument in this example represents the name of the student, Jane Doe. The value "Jane Doe" is a string literal. String literals represent object instances of the predefined Java class java.lang.String. Simply put, string literals are Java's representation of pieces of text.

Soon you will build code for the Student class. At that time, you will specify what to do with this String argument. There are a few things you can do with arguments: You can use them as input data to another operation, you can store them for later use and/or retrieval, you can ignore them, and you can pass them to other objects.

When the Java VM executes the new operator in this statement, it allocates an area in memory to store a representation of the Student object. The VM uses information in the Student class definition to determine just how much memory to allocate.



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