Hi , i'd like to know how is it posible to use "this" keyword for static variables ? I know i am not allowed to use it for methods but how come it's allowed for variables ? If there is no "this" static members how come it's used for them ? I am confused with this. Thanks...
no i did not understand it. When using this code , if we do create an instance of this class in constructor we are allowed to use "this" to a static members, why ? i read that it's not allowed to use it since "this" refers to an object and methods must belong to an object in order to use "this" on them. let's view this.
this.i is equivalent to StaticTest.i Since this has the type of StaticTest in your code. All it matters is the type for static fields or methods. What I gave you is the extreme case, even null, if it has the right type, it still works. [This message has been edited by Roseanne Zhang (edited February 23, 2001).]
no you haven't understood , or it's not what i wanted to hear. What answer i was looking for is that you are not allowed a use of "this" keyword inside a static methods or blocks because it's not allowed for them.
You cannot access this in a static method, correct!!! Which does not imply you cannot access static variable in a non-static method. Through this is unnecessary, even stupid, bad coding style, but legal. [This message has been edited by Roseanne Zhang (edited February 23, 2001).]
Guys , i don't argue that that is stupid to use it, i undertand that my confusion came to when i saw it in code used on one of the exams. And i answered it wrong so i wanted to make sure i understood where it was allowed to be used and where prohibited.
Roseanne, I looked for your pic proving you weren't a guy, but alas no lass to see! ;-) I guess we'll have to trust you on this one. Thanks for the link, I hadn't seen the contacts page before and this is after 6 months of JavaRanch readership! Octavyn
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop