Professional Documents
Culture Documents
D67234GC20 Les06
D67234GC20 Les06
6-2 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Topics
6-3 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Working with Objects: Introduction
6-4 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Accessing Objects by Using a Reference
6-5 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Shirt Class
6-6 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Topics
6-7 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Working with Object Reference Variables
Declaration:
Classname identifier;
This code fragment
Instantiation: creates the object.
new Classname();
Assignment:
Object reference = new Classname();
The identifier
The To assign to a reference,
from the
assignment creation and assignment
Declaration
operator must be in same statement.
step
6-8 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Declaring and Initializing: Example
1 Declare a
reference for
the object. 2
Create the
object instance.
Shirt myShirt;
3 Assign the
object to the
reference
variable.
6-9 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Working with Object References
Declare and
initialize
reference.
Call the
display()
method of
the object.
1 Create a Shirt
object and get a
reference to it.
2 2
Press remote Call a method to
controls to have Shirt object
have camera do something.
do something.
remote1.play();
remote2.stop();
remote1
Television
Television
remote
Camcorder
Camcorder
remote
0x034009
Stack shirtID Heap
12
15.99 price
B colorCode
counter 10
0x99f311
myShirt 0x034009 12 shirtID
15.99 price
yourShirt 0x99f311
colorCode
B
myShirt = yourShirt;
12
15.99
B
counter 10
0x99f311
myShirt 0x99f311 12 shirtID
15.99 price
yourShirt 0x99f311
colorCode
B
Code fragment:
myShirt = yourShirt;
myShirt.colorCode = 'R';
yourShirt.colorCode = 'G';
Shirt color: G
myShirt.colorCode = 'R';
yourShirt.colorCode = 'G';
12
15.99
B
counter 10
0x99f311
myShirt 0x99f311 12 shirtID
15.99 price
yourShirt 0x99f311
colorCode
G
0x034009
Hello
myString 0x034009
0x034009
"Hello"
0x99f311
myString 0x99f311
"Hello World"
0x99f311
"Hello World"
0x74cd23
myString 0x74cd23
"Hello World!"
Depending on what
you select, either
the classes in a
particular package
or all classes are
listed here.
The name of
the method
Some of the
methods of
PrintStream
String
String concatenation
myString = "Hello";
myString = myString.concat("
• Costly World);
in terms of creating new objects
0x99f311
"Hello "
0x74cd23
myString 0x74cd23
"Hello World"
0x034009
"Hello"
mySB 0x034009
0x034009
"Hello World"
mySB 0x034009