Java Reference
In-Depth Information
1.12
Another example
In this chapter, we have already discussed a large number of new concepts. To help in un-
derstanding these concepts, we shall now revisit them in a different context. For this, we use
another example. Close the house project if you still have it open, and open the lab-classes
project.
This project is a simplified part of a student database designed to keep track of students in labo-
ratory classes and to print class lists.
Exercise 1.21 Create an object of class Student . You will notice that this time you are
prompted not only for a name of the instance, but also for some other parameters. Fill them in
before clicking OK. (Remember that parameters of type String must be written within double
quotes.)
1.13
Return values
As before, you can create multiple objects. And again, as before, the objects have methods
that you can call from their pop-up menu.
Concept:
Exercise 1.22 Create some student objects. Call the getName method on each object.
Explain what is happening.
Result. Methods
may return infor-
mation about an
object via a return
value.
When calling the getName method of the Student class, we notice something new: methods
may return a result value. In fact, the signature of each method tells us whether or not it returns
a result and what the type of the result is. The signature of getName (as shown in the object's
pop-up menu) is defined as
String getName()
The word String before the method name specifies the return type. In this case, it states that
calling this method will return a result of type String . The signature of changeName states:
void changeName(String replacementName)
The word void indicates that this method does not return any result.
Methods with return values enable us to get information from an object via a method call. This
means that we can use methods either to change an object's state or to find out about its state.
1.14
Objects as parameters
Exercise 1.23 Create an object of class LabClass . As the signature indicates, you need to
specify the maximum number of students in that class (an integer).
 
 
 
Search WWH ::




Custom Search