Hi all, I am confused regarding the usage of "this" keyword. I will try to explain what I understood about "this". Please correct me if I'm wrong. Case I:
In above scenario we have to use keyword "this" to access the class's variable "name". Case II:
But there are some examples that used "this.name" in the return statement instead of just name. Is there any particular reason in doing so? I do not understand why we need to use "this" keyword here. I will give sample code from Max's book so that it will be easy for anyone to explain based on the example.
I really do not understand the need for using keyword "this" here. Not only in the book example, but have seen some of the code here(especially GUI, I think) that uses "this" though there is no local variable hiding the instance variable. Does it relates to any threading concepts? Can anyone please explain this "this"? Thanks alot. I read K&B book again regarding it, but this is all what I understood. Appreciate your help, Thanks.
Thanks Pavel. I got one more doubt too... Here is the relevant code from Sun's exaple TableDemo
The only two places DEBUG is used are shown. DEBUG is "false". What is the purpose of making DEBUG false, and using it in coditional statements if there is no other way that DEBUG is going to be true. Guys...am confused. As you can see, need some help. Thanks.
The first thing you can see is that the DEBUG boolean variable is in capital letters. So this implies that it will not be changed further in the code. And the reason is that after the code is debugged, the variable is initialied to false and no debug info appears during execution. The class variable is one, and there is no need to change every if statement.
Hi Satish, DEBUG is being used here as a very crude form of conditional compilation. I'd bet whoever did this has a background in the C language. If you set DEBUG = false in one place you effectively won't enter the code enclosed in the any of the if (DEBUG) statements. If you were to set DEBUG = true, then you would. It's supposed to be an easy way to turn on and off debugging output. Of course, with the current version of Java this could be more effectively done with logging or assertions. The "this" thing is mostly a matter of style. Some Java style guides recommend that all references to member instance variables include the this reference. Of course, it makes it clear which variables are instance variables. It's absolutely needed in the idiom that you quoted before:
because the variable name of the innermost enclosing scope takes precedence over the use of the same name in any outer enclosing scope. It's the only way to resolve the data hiding correctly in this case. Any other use of "this" with an instance variable is a matter of style. That is, emphasizing that the variable is an instance variable. Actually any time you reference an instance variable you are implicitly referencing the current instance of the class containing the instance variable, in other words, the "this" instance. So, whether you make the reference explicit is largely a matter of style (except if you need to resolve a data hiding situation).