• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

keyword "this"

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call a static data member or method, all it matters is right type. Obviously this has the right type. See example here

Thanks!
Roseanne
Join our SCJD Study Group when certified
[This message has been edited by Roseanne Zhang (edited February 22, 2001).]
 
Ruslan Ivanov
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can access the static variable with either:
StaticTest.i; which is equivalent to
this.i; (here "this" refers to the class itself)
or
StaticTest t = new StaticTest();
t.i;
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or
StaticTest t = null;
t.i;
System.out.println(t.i);
You can, but do not have to instantiate a StaticTest to access it.
That is the important point!!!

[This message has been edited by Roseanne Zhang (edited February 23, 2001).]
 
Ruslan Ivanov
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This refers to a particular instance of the class. Static methods can be called with NO instance of the class created. There is no object for "this" to be.
 
Ruslan Ivanov
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just FYI, Cindy and I are not guys.
See here! http://www.javaranch.com/contact.jsp
[This message has been edited by Roseanne Zhang (edited February 23, 2001).]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic