0% found this document useful (0 votes)
38 views33 pages

06 This

Uploaded by

peachypaimon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views33 pages

06 This

Uploaded by

peachypaimon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

CS 102

Object Oriented Programming

this
this
2

¨ An important keyword used inside methods in order


to refer to the current object.
¨ this is “the calling object”
¤ this is the object which called the method
¤ The object that occurs before the dot

Ozyegin University - CS 102 - Object Oriented Programming


this
3

¨ There are several use cases of this

Ozyegin University - CS 102 - Object Oriented Programming


Bank Account
4

Ozyegin University - CS 102 - Object Oriented Programming


Bank Account
5

Ozyegin University - CS 102 - Object Oriented Programming


this
6

¨ There are several use cases of this


1. It is used to access the instance variables without any
confusion with parameter names.

Ozyegin University - CS 102 - Object Oriented Programming


this
7

¨ There are several use cases of this


1. It is used to access the instance variables without any
confusion with parameter names.

Ozyegin University - CS 102 - Object Oriented Programming


this (first use case)
8

¨ One letter parameters are not very descriptive.


¤ It is usually the convention in Java to use the instance
variable name as parameters in the corresponding set
methods.
Ozyegin University - CS 102 - Object Oriented Programming
this (first use case)
9

¨ It should be smt like:

¤ parameter names are same with class instance names


¤ no compile error
Ozyegin University - CS 102 - Object Oriented Programming
this (first use case)
10

¨ What should
be the output?

Ozyegin University - CS 102 - Object Oriented Programming


this (first use case)
11

¨ What should
be the output?

Ozyegin University - CS 102 - Object Oriented Programming


this (first use case)
12

¨ It gives warning as : The assignment to variable


interesRate has no effect.
¨ When class instance and parameter have the same
names, the assignment operation has no effect

Ozyegin University - CS 102 - Object Oriented Programming


this (first use case)
13

¨ The parameters interestRate and currency shadow


the class instances.

¨ The interestRate assigns the parameter interestRate


to itself, causing no change what so ever.

Ozyegin University - CS 102 - Object Oriented Programming


this (first use case)
14

¨ The parameters interestRate and currency shadow


the class instances.

¨ In such cases you can use the this keyword to reach


the class instances.

Ozyegin University - CS 102 - Object Oriented Programming


this (first use case)
15

Ozyegin University - CS 102 - Object Oriented Programming


this (first use case)
16

¨ What should
be the output?

Ozyegin University - CS 102 - Object Oriented Programming


this (first use case)
17

¨ What should
be the output?

Ozyegin University - CS 102 - Object Oriented Programming


this (first use case)
18

¨ It can be also used


inside constructors
to seperate class
instances from
parameters.

Ozyegin University - CS 102 - Object Oriented Programming


First Constructor
19

Ozyegin University - CS 102 - Object Oriented Programming


Second Constructor
20

Ozyegin University - CS 102 - Object Oriented Programming


Third Constructor
21

Ozyegin University - CS 102 - Object Oriented Programming


this (first use case)
22

¨ To be consistent, the same can be done in get


methods.

Ozyegin University - CS 102 - Object Oriented Programming


A conceptual model for this account1
23

¨ A conceptual model for


understanding this
number
¨ Each object in Java has an 1
implicit instance variable,
balance
named this, that points to
100
the object itself.
currency
“TL”
this

Ozyegin University - CS 102 - Object Oriented Programming


A conceptual model for this account1
24

¨ This is not what really


happens during execution.
number
¨ In Java, each object does
NOT keep a reference to 1
itself – that would be a balance
waste of memory space. 100
¨ However, take this as simply currency
a conceptual model “TL”
that helps us understand this
what this means.

Ozyegin University - CS 102 - Object Oriented Programming


this
25

¨ There are several use cases of this


1. It is used to access the instance variables without any
confusion with parameter names.
2. It can be used to invoke constructors of the same class.

Ozyegin University - CS 102 - Object Oriented Programming


this (second use case)
26

¨ When you have multiple constructors, this keyword


can be used to call other constructors.

Ozyegin University - CS 102 - Object Oriented Programming


Three Constructors
27

Ozyegin University - CS 102 - Object Oriented Programming


this (second use case)
28

Ozyegin University - CS 102 - Object Oriented Programming


this (second use case)
29

Ozyegin University - CS 102 - Object Oriented Programming


Using this for constructor call
30

¨ The constructor call should be the first statement in


your constructor.
¨ Otherwise you will receive the following error!

Ozyegin University - CS 102 - Object Oriented Programming


31

¨ You cannot use the constructor of the class inside a


constructor to call another constructor.
¨ The only way of calling another constructor within a
constructor is through using this keyword.
Ozyegin University - CS 102 - Object Oriented Programming
Ozet
32

¨ this:
¤ Class instancelarini
(member degiskenlerini),
fonksiyon parametreleri
ile karistirmamak icin.

¤ Constructor in icerisinde
diger bir constructori
cagirabilmek icin.
n Bu durumda cagiran
constructorin ilk cumlesi
“this….“ olmak zorunda
yoksa hata verir.

Ozyegin University - CS 102 - Object Oriented Programming


33 Any Questions ?

Ozyegin University - CS 102 - Object Oriented Programming

You might also like