Java Reference
In-Depth Information
Consider the following method main :
public class CascadedMethodCalls
{
public static void main(String[] args)
{
Person student1 =
new Person("Angela", "Smith");
//Line 1
Person student2 = new Person();
//Line 2
Person student3 = new Person();
//Line 3
System.out.println("Line 4 -- Student 1: "
+ student1);
//Line 4
student2.setFirstName("Shelly").
setLastName("Malik");
//Line 5
System.out.println("Line 6 -- Student 2: "
+ student2);
//Line 6
student3.setFirstName("Chelsea");
//Line 7
System.out.println("Line 8 -- Student 3: "
+ student3);
//Line 8
student3.setLastName("Tomek");
//Line 9
System.out.println("Line 10 -- Student 3: "
+ student3);
//Line 10
}
}
Sample Run:
Line 4 -- Student 1: Angela Smith
Line 6 -- Student 2: Shelly Malik
Line 8 -- Student 3: Chelsea
Line 10 -- Student 3: Chelsea Tomek
The statements in Lines 1, 2, and 3 declare the variables student1 , student2 , and
student3 and also instantiate the objects. The instance variables of the objects student2
and student3 are initialized to empty strings. The statement in Line 4 outputs the value
of student1 . The statement in Line 5 works as follows. In the statement:
student2.setFirstName("Shelly").setLastName("Malik");
first the expression:
student2.setFirstName("Shelly")
Search WWH ::




Custom Search