Class EmployeeTest
EmployeeTest
method main
(Fig. 8.13) instantiates two Employee
objects (lines 13–14). When each Employee
object’s constructor is invoked, lines 15–16 of Fig. 8.12 assign the Employee
’s first name and last name to instance variables firstName
and lastName
. These two statements do not make copies of the original String
arguments. Actually, String
objects in Java are immutable—they cannot be modified after they’re created. Therefore, it’s safe to have many references to one String
object. This is not normally the case for objects of most other classes in Java. If String
objects are immutable, you might wonder why we’re able to use operators +
and +=
to concatenate String
objects. String-concatenation actually results in a
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.