Title: Java programs are built from classes.
1(No Transcript)
2Java programs are built from classes.
3Java programs are built from classes. There is
nothing else, but classes.
4(No Transcript)
5A class contains members
6A class contains members a) data (variables)
7A class contains members a) data (variables)
b) procedures (methods)
8(No Transcript)
9Members could be static or not static.
10(No Transcript)
11End of story.
12Lets see some examples now.
13Draw this
class One
14Draw this
Dont forget the blueprint (even though its
empty).
class One
One
15Draw this
class Two int u double q
16Draw this
Notice that the blueprint is no longer empty
Two
class Two int u double q
17Draw this
Notice that the blueprint is no longer empty
Two
class Two int u double q
u
q
18Draw this
Notice that the blueprint is no longer empty
Two
class Two int u double q
u
q
u and q are called instance variables
19Draw this
class Student
20Draw this
Student
class Student
Easy. Student or One same difference.
21Now draw this
class Student int age
22Now draw this
Student
class Student int age
age
23Draw this
class Z int m double n
24Draw this
Z
class Z int m double n
m
n
25Draw this
class M int m static double n
26Draw this
M
class M int m static double n
n
m
27Draw this
M
class M int m static double n
m
n
28Draw this
M
class M int m double n
m
n
29Draw this
M
class M int m static double n
n
m
30Draw this
class Q int v double z public
static void main(String args)
31Draw this
class Q int v double z public
static void main(String args)
For this the picture is bigger
32Draw this
class Q int v double z public
static void main(String args)
Q
v
z
main
args
For this the picture is bigger
33Draw this
class Q int v double z public
static void main(String args)
Q
v
z
main
args
For this the picture is bigger
34Draw this
class Q int v double z public
static void main(String args)
Q
v
z
main
args
For this the picture is bigger
35Draw this
class E int v double z public
static void main(String args) int q
q 3
36Draw this
class E int v double z public
static void main(String args) int q
q 3
Same as above, only main is no longer empty
37Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
Same as above, only main is no longer empty
38Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
Same as above, only main is no longer empty
39Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
Same as above, only main is no longer empty
40Draw this
q is a local variable.
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
Same as above, only main is no longer empty
41Draw this
q is a local variable. It is local to main.
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
Same as above, only main is no longer empty
42Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
q 3
Same as above, only main is no longer empty
43Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
q 3
Same as above, only main is no longer empty
44Draw this
class E int v double z public
static void main(String args) int q
q 3
E
v
z
main
args
int q
3
q 3
Same as above, only main is no longer empty
45Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
46Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
age
gpa
main
args
Student
47Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
age
gpa
main
args
q
Student
48Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
age
gpa
main
args
q
21
Student
49Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
age
gpa
main
args
q
21
n
Student
50Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
age
gpa
main
args
q
21
n
Student
51java.lang.String
Draw this
class Student int age double gpa
public static void main(String args)
int q 21 String n John
John
age
gpa
main
args
q
21
n
Student
52java.lang.String
Draw this
String is a reference type.
class Student int age double gpa
public static void main(String args)
int q 21 String n John
John
age
gpa
main
args
q
21
n
Student
53Types in Java
54Types in Java a) primitive
55Types in Java a) primitive (int, double,
boolean, char)
56Types in Java a) primitive (int, double,
boolean, char) b) reference types
57Types in Java a) primitive (int, double,
boolean, char) b) reference types (classes)
58Types in Java a) primitive (int, double,
boolean, char)
int x
59Types in Java a) primitive (int, double,
boolean, char)
int x
int y
60Types in Java a) primitive (int, double,
boolean, char)
int x
int y
x 5
61Types in Java a) primitive (int, double,
boolean, char)
int x
int y
x 5
62Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
x 5
63Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
x 5
y x
64Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
x 5
y x
65Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
66Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
x x 2
67Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
x x 2
68Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
x 5 2
69Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
x 5 2
70Types in Java a) primitive (int, double,
boolean, char)
int x
5
int y
5
x 5
y x
x 5 2
71Types in Java a) primitive (int, double,
boolean, char)
int x
7
int y
5
x 5
y x
x x 2
72Types in Java a) primitive (int, double,
boolean, char)
int x
7
But this doesnt change y.
int y
5
x 5
y x
x x 2
73Types in Java a) primitive (int, double,
boolean, char)
int x
7
But this doesnt change y. And thats because y
is a copy of x.
int y
5
x 5
y x
x x 2
74Types in Java a) primitive (int, double,
boolean, char)
int x
7
But this doesnt change y. And thats because y
is a copy of x. Things dont work the same with
reference types.
int y
5
x 5
y x
x x 2
75Types in Java a) primitive (int, double,
boolean, char)
int x
7
But this doesnt change y. And thats because y
is a copy of x. But the above is true of
primitive types int (long, short,
byte) double (float) char boolean
int y
5
x 5
y x
x x 2
76Types in Java a) primitive (int, double,
boolean, char) b) reference types
77Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
78Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
79Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
80Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
81Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
82Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
83Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
84Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
85Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
x.turnLeft()
86Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
x.turnLeft()
87Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
y x
x.turnLeft()
88Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
But that also changes the value of y.
y x
x.turnLeft()
89Types in Java a) primitive (int, double,
boolean, char) b) reference types
Penguin x
x new Penguin()
Penguin y
But that also changes the value of y.
y x
The Penguin object is only one, and shared.
x.turnLeft()
90Types in Java a) primitive (int, double,
boolean, char) b) reference types
91Types in Java a) primitive (int, double,
boolean, char) b) reference types (classes)
92Types in Java a) primitive (int, double,
boolean, char) b) reference types (classes)
A value of primitive type fits in a location of
that type.
93Types in Java a) primitive (int, double,
boolean, char) b) reference types (classes)
A value of primitive type fits in a location of
that type.
A value of reference type (an object) doesnt.
94Types in Java a) primitive (int, double,
boolean, char) b) reference types (classes)
A value of primitive type fits in a location of
that type.
A value of reference type (an object)
doesnt. For objects, a reference to them is
kept in the variable.
95Lets continue with the examples.
96Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
97Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
main
args
J
98Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
main
args
z
J
99Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
J
100Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
J
101Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
J
102Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
5
J
103Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
5
J
104Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
5
J
105Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
5
J
106Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
0
main
args
z
x
5
J
107Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
5
main
args
z
x
5
J
108Draw this
class J int x public static void
main(String args) J z new J()
int x 5 z.x 5
x
x
5
main
args
z
x
5
J
109Draw this
110Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
111Draw this
What kind of variables are age and gpa?
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
112Draw this
Yes, they are instance variables.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
113Draw this
What kind of variables are q, n, and s?
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
114Draw this
Exactly, they are local variables.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
115Draw this
Theyre local to main.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
116Draw this
Now lets draw!
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
117Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
age
gpa
main
q
args
Student
118Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
age
gpa
main
q
args
n
Student
119Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
age
gpa
Student
120Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
121Draw this
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
122Draw this
Notice that q and n have no values in them.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
123Draw this
Notice that q and n have no values in
them. Unlike instance or class variables, local
variables need to be initialized explicitly by
the programmer.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
124Draw this
Instance and class variables have default values.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
125Draw this
Instance and class variables have default
values. Its not hard to guess the default
values.
class Student int age double gpa
public static void main(String args)
int q String n Student s
s new Student()
0
age
age
gpa
0
gpa
Student
126Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
127Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
gpa
main
args
Student
128Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
20
age
gpa
0
gpa
main
args
s
Student
129Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
age
20
20
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
130Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
age
20
20
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
131Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
age
20
20
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
132Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
age
20
20
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
133Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
age
20
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
134Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
age
20
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
135Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
136Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
137Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
138Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 19
age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
139Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 22
age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
140Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 22
age
age
21
19
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
141Draw this
class Student int age 20 double
gpa public static void main(String args)
Student s new Student()
Student q new Student() q.age 19
s.age 21 q.age 3 q.age
age
age
21
22
age
gpa
gpa
0
0
gpa
main
args
s
q
Student
142Now write this (in Java) Create two accounts,
for Doug and Adrian.
143Now write this (in Java) Create two accounts,
for Doug and Adrian. Initial balance for both
accounts is 10.
144Now write this (in Java) Create two accounts,
for Doug and Adrian. Initial balance for both
accounts is 10. Then Dougs account doubles,
Adrians decreases by 3 dollars, and Dougs
account has one more dollar deposited into it.
145Let me try it. How about this?
146Let me try it. How about this?
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
147Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
148Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
149Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
150Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
151Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
152Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
153Create two accounts, for Doug and Adrian. Initial
balance for both accounts is 10. Then Dougs
account doubles, Adrians decreases by 3 dollars,
and Dougs account has one more dollar deposited
into it.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
154balance
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
Account
155Looks good.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
156Looks good.
Thank you.
class Account double balance 10
public static void main(String args)
Account doug, adrian doug new
Account() adrian new Account()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
157Draw this
158Draw this
class BankAccount double balance 25
public static void main(String args)
BankAccount doug new BankAccount()
BankAccount adrian new BankAccount()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
159Draw this
Too easy, almost the same thing...
class BankAccount double balance 25
public static void main(String args)
BankAccount doug new BankAccount()
BankAccount adrian new BankAccount()
doug.balance doug.balance 2
adrian.balance - 3 doug.balance
160Draw this
161Draw this
class Account double balance 20
void deposit(int n) this.balance n
162Draw this
class Account double balance 20
void deposit(int n) this.balance n
Account
this.balance
deposit
n
this.balance n
163Draw this
class Account double balance 20
void deposit(int n) this.balance n
Account
this.balance
deposit
n
this.balance n
164Draw this
balance is an instance variable.
class Account double balance 20
void deposit(int n) this.balance n
Account
165Draw this
balance is an instance variable. It belongs to
the object.
class Account double balance 20
void deposit(int n) this.balance n
Account
166Draw this
deposit is an instance method. It belongs to the
object and describes an update
on its instance variables (state).
class Account double balance 20
void deposit(int n) this.balance n
Account
167Draw this
n is not a local variable in deposit.
class Account double balance 20
void deposit(int n) this.balance n
Account
168Draw this
It is called a parameter of the method
deposit. It is a way of naming the methods
inputs. It is a way to refer to the ingredients
of the recipe.
class Account double balance 20
void deposit(int n) this.balance n
Account
169Draw this
The keyword this allows an object to refer to
itself.
class Account double balance 20
void deposit(int n) this.balance n
Account
170Draw this
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
Account
171Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
main
args
172Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
main
args
m
173Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20
main
args
m
q
174Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20
main
args
m
q
175Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20
main
args
m
q
176Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20
main
args
m
q
177Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20
main
args
m
q
178Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
20
20
10
main
args
m
q
179Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
30
20
main
args
m
q
180Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
30
20
main
args
m
q
181Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
30
20
main
args
m
q
182Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
30
20
3
main
args
m
q
183Draw this
Account
class Account double balance 20
void deposit(int n) this.balance n
public static void main(String
args) Account m new Account()
Account q new Account()
m.deposit(10) q.deposit(3)
30
23
main
args
m
q
184Draw this
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
185Draw this
This is a constructor.
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
186Draw this
Its like an initialization procedure.
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
187Draw this
Its attached to the blueprint
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
188Draw this
Its attached to the blueprint (not instances)
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
189Draw this
Its attached to the blueprint (not
instances) This one is empty.
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
190Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
this.balance
Account
x
main
args
191Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
this.balance
Account
10
x
0
balance
main
args
m
192Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
this.balance
Account
x
0
balance
main
args
m
193Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
this.balance
Account
x
0
0
balance
balance
main
args
m
q
194Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
this.balance
Account
x
0
0
balance
balance
main
args
m
q
195Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
this.balance
Account
x
0
0
balance
balance
main
args
m
q
10 m.balance
196Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
this.balance
Account
x
0
0
balance
balance
main
args
m
q
10 0
197Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
this.balance
Account
x
0
0
balance
balance
main
args
m
q
10
198Draw this
Account
class Account double balance
Account(double x) public static
void main(String args) Account m
new Account(10) Account q new
Account(3) m.balance 10 m.balance
this.balance
Account
x
0
10
balance
balance
main
args
m
q
199Draw this
class X int x
200How easy.
Draw this
X
class X int x
x
201Draw this
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
202Draw this
Thats called a constructor.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
203Draw this
Thats called a constructor.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
204Draw this
Notice the name of the constructor.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
205Draw this
Notice the name of the constructor. Its the name
of the class.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
206Draw this
Notice the name of the constructor. Its the name
of the class. A constructor looks like a method
(procedure).
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
207Draw this
Notice the name of the constructor. Its the name
of the class. A constructor looks like a method
(procedure). But doesnt have a return type.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
208Draw this
Heres how the constructor works
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
209Draw this
Heres how the constructor works
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
this.x initialValue
210Draw this
When the constructor is invoked, a value is
passed (as an argument) to the constructor.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
this.x initialValue
211Draw this
The constructor then starts executing.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
this.x initialValue
212Draw this
The constructor then starts executing. The value
is taken from the argument,
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
this.x initialValue
213Draw this
The constructor then starts executing. The value
is taken from the argument, into the instance
variable.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
A
initialValue
this.x initialValue
214Draw this
Thus the instance variable will have
been initialized with what is in initialValue.
class A int x A (int initialValue)
this.x initialValue / do this
when you first create the object but
dont make it part of the generated instance /
A
x
initialValue
A
initialValue
this.x initialValue
215Speaking of this, can I show you an example?
216Sure, go ahead.
217Sure, go ahead.
Here it is
class Speakers public static void
main(String args) Speaker a new
Speaker(Larry) Speaker b new
Speaker(Michael) Speaker c new
Speaker(Toni) a.talk()
b.talk() c.talk() class
Speaker String speaker Speaker(String
name) speaker name void
talk() System.out.println(this.speaker
is very happy.)
218This writer is very happy.
Here it is
class Speakers public static void
main(String args) Speaker a new
Speaker(Larry) Speaker b new
Speaker(Michael) Speaker c new
Speaker(Toni) a.talk()
b.talk() c.talk() class
Speaker String speaker Speaker(String
name) speaker name void
talk() System.out.println(this.speaker
is very happy.)
219This writer is very happy.
Ha, ha. Draw this
class Student String name
Student(String w) this.name w
class Tuesday public static void
main(String args) Student q new
Student(Larry) Student u new
Student(John) Student s new
Student(Adrian)
220Thats funny.
Ha, ha. Draw this
class Student String name
Student(String w) this.name w
class Tuesday public static void
main(String args) Student q new
Student(Larry) Student u new
Student(John) Student s new
Student(Adrian)
221Thats funny.
Yes.
class Student String name
Student(String w) this.name w
class Tuesday public static void
main(String args) Student q new
Student(Larry) Student u new
Student(John) Student s new
Student(Adrian)
We should always try to refer to instance
variables through the instance they belong to.
222Thats funny.
Yes.
class Student String name
Student(String w) this.name w
class Tuesday public static void
main(String args) Student q new
Student(Larry) Student u new
Student(John) Student s new
Student(Adrian)
We should always try to refer to instance
variables through the instance they belong
to. This way we would also be able to keep them
separate from local variables and
parameters (or arguments).
223Draw this
class Checking double balance
Checking(double x) this.balance x
void deposit(double y)
this.balance this.balance y
224Draw this
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
225Draw this
instance variable
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
226Draw this
instance method
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
227Draw this
formal parameter
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
228Draw this
formal parameter (like a local variable only
initialized when invoked)
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
229Draw this
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
constructor
230Draw this
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
231Draw this
class Hat public static void main(String
args) Tea m new Tea(-2)
Tea n new Tea(42) m.book(92)
n.bird m.bird n.bird n.book(25)
n.book(25)
232Draw this
class Hat public static void main(String
args) Tea m new Tea(-2)
Tea n new Tea(42) m.book(92)
n.bird m.bird n.bird n.book(25)
n.book(25)
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
Where Tea is the class seen before.
233Draw this
class Hat public static void main(String
args) Tea m new Tea(-2)
Tea n new Tea(42) m.book(92)
n.bird m.bird n.bird n.book(25)
n.book(25)
Where Tea is the class seen before.
class Tea double bird void
book(double watch) this.bird watch
this.bird Tea (double snake)
this.bird snake 2
(Soliloquy p. 133) We should also use BlueJ to
examine this example thoroughly.
234Last example
class X int q class Play public
static void main(String args) X u
new X() X p new X() u.q
3 p.q u.q p.q u.q u.q
p.q
235Last example
This should be easy, by now.
class X int q class Play public
static void main(String args) X u
new X() X p new X() u.q
3 p.q u.q p.q u.q u.q
p.q
236(No Transcript)
237So what types do we have in Lab Eight (for
example)?
238So what types do we have in Lab Eight (for
example)?
Point
239So what types do we have in Lab Eight (for
example)?
Point Circle
240So what types do we have in Lab Eight (for
example)?
Point Circle Rectangle
241So what types do we have in Lab Eight (for
example)?
Point Circle Rectangle
We should also discuss Robot (to understand
Penguin).
242Thank you.
243Thank you.