Every time I see the this pointer in Java, I can't figure it out. I have read many people's explanations on the Internet, but I still don't know what the this pointer is. Today's log shows you who this is. (Content from: http://www.mathcs.emory.edu /~ Cheung/Courses/170.2010/Syllabus/03/implicit-param.html)
Abstract: The this pointer is an Implicit Parameter. What is Implicit Parameter.
Implicit Parameter
- Parameters toInstanceMethod
- Recall that a parameterIs a value that is given to a method as input-see: click here
- Methods can have one or moreParameters
- You can pass one or more inputs into a method by specifying the value as parameters when you invoke the method.
- We have learned that instance methodsHave two different kindsOf parameters:
- ExplicitParameterThat is passed by specifying the parameterIn the parenthesis of a method call.
- ImplicitParameterThat is passed by specifying an object variable (object reference)Before the name of a method.
- ExplicitParameter
- Review:
Parameters between the brackets/parenthesisOf the method call are calledExplicitParameters |
- Examples:The values in are explicit parameters
harrysChecking.deposit(); harrysChecking.withdraw(); box.translate(, ); double x = 4.0, y = 6.0; box.translate(, ); |
- NOTE: these statement must be contained in some method, and most likely, they will be in different methods (because their actions are kinda "unrelated ". I have omitted the method for brew.just want to show you how to identify explicit parameters
- AccessingExplicitParameter variable inside the method
- AccessingExplicitParameter variable inside a method is very simple:
To useExplicitParameter variable inside a method, you simply refer the parameter variable by its name
- Example:
public void deposit(double ) { balance = balance + ; } |
(Because this is so trivial, I did not dwell on it when we discuss the implementation of the methods.
I dwell on it now because I will show you how to accessImplicitParameter variable next .)
- ImplicitParameter
- AccessingImplicitParameter inside the method
- There is exactly ONE ImplicitParameter variable in every method.
- Java has assigned a special name (a keyword)To identify thisImplicitParameter variable
- TheKeywordThat Java (and C ++) uses to refer toImplicitParameter variable is calledThis
It must be written with all lower case letters
- The obvious question that you will be asking is:
Question:
- What value doesImplicitParameter variableThisContain ???
- You can figure the answer to this question by using analogy...
Consider the following two pieces of program fragments:
public void deposit(double ) { balance = balance + ; } |
(1) harrysChecking.deposit(700); (2) momsSaving.deposit(500); |
Question:
What is the value of (ExplicitParameter variable) amount In the case (1 )?
AmountQuestion:
And what is the value of (ExplicitParameter variable) amount In the case (2 )?
Amount
- OK, it's time to apply the analogy:
Question:
What is the value of (ImplicitParameter variable) this In the case (1 )?
ThisQuestion:
And what is the value of (ImplicitParameter variable) this In the case (2 )?
ThisHere is a pictorial representation on what's going on inside the computer whenMomsSaving. deposit (500)Is called:
- As you know, object variables suchHarrysCheckingAndMomsSavingAre used to locate the instance variablesOf an object-see: click here
Since the object variable (HarrysCheckingIn case (1) andMomsSavingIn case (2) is passed toImplicitParameter variable ThisIn the method call, the method can useThisTo obtain the correctInstance variables!!!
(That's how the magic works ....)
- Important Fact:
TheImplicitParameter variableThisIs an object reference variableAnd is used to locate the instance variables of the object |
- It remains to show youHowTheImplicitParameter variable ThisIs used...
Example:
public void deposit(double amount) { = + amount; } |
public void deposit(double amount) { . = . + amount; } |
- A case that necessitate the useThis
- DEMO Program:(BankAccount classThis)
- BankAccount. java file: click here
- BankAccountTester. java file: click here
- Compile:Javac BankAccountTester. java
- Run:Java BankAccountTester
- Now editBankAccount. javaAnd removeThis., Compile and run the program again...
The methodDeposit ()Can no longer increase the balance inBankAccountObject...
- Question: why not ???
The nameBalanceMatches the name ofParameter variableAnd thus, the nameBalanceRefers toParameter variableAndNotToInstance variable