and
Classes
Members,
variables,
and
parameters
About
reference
types
Announcements
A
new
format
for
this
Thursdays
quiz
Open
book,
take-home
quiz
Due
Friday
in
lab
NO
collaboraPon
allowed.
Inside
an
instance
method
of
the
class,
we
just
use
the
member
directly
e.g.
name(),
knowing
that
it
refers
to
the
current
object
G.
Ayorkor
Korsah,
Ashesi
University
College,
Programming
II
-
First
Semester
2011
4
Example
Class
deniPon:
public class Student {! private String name;! ! public void setName(String n){! !name = n;! }! ! public String getName() {! !return name;! }! ! public void sayHello() {! !System.out.println(Hello, I am + getName());! }! }!
NoPce
that
inside
the
instance
methods
of
the
class,
we
refer
to
member
variables
and
methods
directly
G.
Ayorkor
Korsah,
Ashesi
University
College,
Programming
II
-
First
Semester
2011
5
Example
public class Test {! ! Class deniPon: public static void main(String[] args){! !Student stud = new Student();! public class Student {! ! private String name;! !stud.setName(Ama);! ! !! System.out.print(Students name is );! public void setName(String n){ !System.out.println(stud.getName());! !name = n;! ! }! !stud.sayHello();! ! public String getName() {! }! }! !return name;! }! ! public void sayHello() {! !System.out.println(Hello, I am + getName());! }! }!
NoPce that inside the instance methods of the class, we refer to member variables and methods directly
NoPce
that
outside
the
class,
we
instanPate
an
object
of
the
class
and
use
a
dot
.
to
refer
to
its
members
6
OpPonally,
inside
the
instance
methods
of
the
class,
we
refer
to
member
variables
using
the
keyword
this!
G.
Ayorkor
Korsah,
Ashesi
University
College,
Programming
II
-
First
Semester
2011
8
Method
parameters
Note
that
the
input
parameters
for
methods
are
similar
to
local
variables
in
that
their
scope
is
within
the
method
for
which
they
are
dened
public class Student {! private String name;! ! public void setName(String n){! !this.name = n;! }! ! public String getName() {! !return this.name;! }! }!
n is an input parameter for the method setName. Its scope is within setName and nothing more.!
11
12
public class Person {! private int age;! ! Person(){! !age = 0;! }! ! public void grow(){! !age++;! }! ! public void growNYears(int n) {! String text = I am + age + yrs old.;! !System.out.println(text);! ! !for (int year=1; year <= n; year++){! ! !grow();! ! !text = In year + year + I am + age;! ! !System.out.println(text);! !}! !text = Done growning.;! !System.out.println(text);! }! }!
13
public class Person {! private int age;! ! Person(){! !age = 0;! }! ! public void grow(){! !age++;! }! ! public void growNYears(int n) {! String text = I am + age + yrs old.;! !System.out.println(text);! ! !for (int year=1; year <= n; year++){! ! !grow();! ! !text = In year + year + I am + age;! ! !System.out.println(text);! !}! !text = Done growning.;! !System.out.println(text);! }! }!
14
15
public class Person {! private int age;! ! Person(){! !age = 0;! }! ! public void readAge(){! !Scanner in = new Scanner(System.in);! !int age = in.nextInt();! }! ! public void grow(){! !age++;! }! ! public int getAge(){! !return age;! }! }!
16
public class Person {! private int age;! ! Person(){! !age = 0;! }! ! public void readAge(){! !Scanner in = new Scanner(System.in);! !int age = in.nextInt();! !this.age = age;! }! ! public void grow(){! !age++;! }! ! public int getAge(){! !return age;! }! }!
17
Warning!!!
The
parameter
name
is
hiding
the
member
variable
name.
Here,
the
right-hand-side
refers
to
the
parameter
name
and
the
leg-hand-side
refers
to
the
member
variable
name.
Warning!!!
Local
variable
name
is
hiding
member
variable
name.
Here,
the
right-hand-side
refers
to
the
local
variable
name
and
the
leg-hand-side
refers
to
the
member
variable
name.
18
19
20
21
Reference
Types
Remember
when
you
use
the
keyword
new
to
create
an
object,
Java
actually
returns
a
reference
to
the
object
You
can
think
of
a
reference
as
a
memory
address
Thus,
this
is
a
fundamental
dierence
between
a
variable
of
a
primiPve
type
and
a
variable
of
a
class
type
A
variable
of
a
primiPve
type
stores
a
value
A
variable
of
a
class
type
stores
a
reference
22
We
know
that
Java
allocates
memory
for
the
two
variables
and
iniPalizes
them
to
their
default
values
Suppose
the
variables
are
allocated
start
at
memory
addresses
1024
and
1028
respecPvely
Memory
address
1024
Memory
address
1028
0 null
13
2048
person
age
height
24