evaluate object reference expressions that use the keyword this
The keyword this can be used in a class to refer to the current calling object.
For example, in the following Class Person, when we create an object p1 and call the constructor or p1.setEmail(), the "this" refers to p1.
And when we make the same method calls with object p2, "this" refers to p2.
Note that in the code above, this.name, this.email, and this.phoneNumber are equivalent to just name, email, and phoneNumber but this.variable is a way to indicate that we are referring to the instance variables of this object instead of a local variable.
Static methods cannot refer to this or instance variables because they are called with the class object, so there is no this object.
The keyword this is sometimes used by programmers to distinguish between variables.
Programmers can give the parameter variables the same names as the instance variiables and this can distinguish them and avoid a naming conflict.
For example, both the instance variable and the parameter variable are called name in the code below.
The this variable can be used anywhere you would use an object variable.
You can even pass it to another method as an argument.
Consider the classes below, Pay and Overtime.
The Pay class declares an Overtime object and passes in this (the current Pay object) to its constructor which computes the overtime with respect to that Pay object.
Here is an image which shows how this and myPay and p all refer to the same object in memory:
SUMMARY
Within a non-static method or constructor, the keyword this is a reference to the current object, the object whose method or constructor is being called.
this.instanceVariable can be used to distinguish between this object's instance variable and local parameter variables that may have the same variable names.
Static methods do not have a this reference.
The this variable can be used anywhere you would use an object variable, even to pass it to another method as an argument.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) Bank Account. Create a class called BankAccount that keeps track of the account holder's name, the account number, and the balance in the account. Make sure to use the appropriate data types for these.
Write 2 constructors for the class that initialize the instance variables; the first should set the default values and the second should take parameters.
For the parameters, make sure to use the same variable names as your instance variables.
Use the keyword this to distinguish between the instance variables and the parameter variables.
Write a toString() method for the class and use the keyword this to return the instance variables.
Write withdraw(amount) and deposit(amount) methods for the class. Withdraw should subtract the amount from the balance as long as there is enough money in the account (the balance is larger than the amount). Deposit should add the amount to the balance. Use the keyword this to refer to the balance.
Test your class with your main method by creating a BankAccount object and calling its deposit and withdraw methods. Print out the object to test its toString() method.