Académique Documents
Professionnel Documents
Culture Documents
1
Today’s Lecture
• Defining Objects
• Parameterized Constructors
2
Constructors
• Member functions can be used to give values to the data items in an object.
• Sometimes we want an object can initialize itself when it’s first created,
3
Declaring Objects
• A line similar to the following is used to declare an object of type Box:
• The first line declares mybox as a reference to an object of type Box. At this
point, mybox does not yet refer to an actual object. The next line allocates
an object and assigns a reference to it to mybox.
4
Declaring Objects
• The assignment of b1 to b2 did not allocate any memory or copy any part of
the original object. It simply makes b2 refer to the same object as does b1.
• Thus, any changes made to the object through b2 will affect the object to
which b1 is referring, since they are the same object.
6
Declaring Objects
• Although b1 and b2 both refer to the same object, they are not linked in any
other way.
Box b2 = b1;
// ...
b1 = null;
• Here, b1 has been set to null, but b2 still points to the original object.
7
Adding Methods to a Class
• Methods define the behavior of a lass.
• This allows the class implementor to hide the specific layout of internal data
• In addition to defining methods that provide access to data, you can also
define methods that are used internally by the class itself.
• In the Box class example, it makes sense to add a method that calculates the
volume inside the class itself instead of calculating it each time outside the
class.
8
Adding Methods to a Class
• So, the class declaration (definition) would be modified to the following:
double width;
double height;
double depth;
• It is called using the new operator. A constructor has no return type, not
even void. This is because the implicit return type of a class’ constructor is
the class type itself.
11
Constructors
• This is mistake prone, because the programmer may forget to initialize the
object after creating it.
12
Constructors
/* Here, Box uses a constructor to initialize the dimensions of a box.
*/
class Box {
double width;
15
Parameterized Constructors
• While the Box( ) constructor in the preceding example does initialize a Box
object, it is not very useful—all boxes have the same dimensions.
16
Parameterized Constructors
/* Here, Box uses a parameterized constructor to initialize the dimensions of a
box.
*/
• To allow this, Java defines the this keyword. this can be used inside any
• That is, this is always a reference to the object on which the method was
invoked.
19
The this Keyword
• Here is another version of Box( ), which uses width, height, and depth for
parameter names and then uses this to access the instance variables by the
same name:
20
Classes and objects in memory
• Actually, things are not quite so simple. It’s true that each object has its own
separate data items.
• On the other hand, contrary to what you may have been led to believe, all
21
Classes and objects in memory