• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

"this" keyword

 
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: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally this in a method means "the object I was invoked on".
You can use the this keyword for any members and attributes on a class.
So yes for static attributes, and yes for methods.
 
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
I don't agree with you. You can't use "this" to invoke static methods simply because there is no "this" for them. They belong to a class not to an object. But what i don't understand is why it's allowed for static variables.
 
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
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.

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<code>
public class Test {
public static void main(String args[]) {
PP obj = new PP();
System.out.println(obj.i);
System.out.println(PP.i);
obj.statp();
PP.statp();
}
}
class PP {
static int i = 10;
int j = 20;
void p() {
System.out.println("instance method in PP");
}
static void statp() {
System.out.println("static method in PP");
}
}
</code>
Actually, you can refer a static member or method by its class name, OR you can refer it using an object reference of that class. Both will refer to the same member or method.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"this" can be used with any member of an object. Any class member is also an instance member but the difference is the class member is common to all instances of the object and any object can manipulate it. Normally, we don't use this.staticvar++, etc. but it is not an errot to use. Only inside static methods, static initializers, there is no this or super keyword allowed.


------------------

***********************************************
Sun Certified Programmer for Java 2 Platform
***********************************************
 
greg philpott
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not saying you can use the keyword "this" to invoke methods, that doesn't make sense.
Your code works fine, do you have a problem with it?
basically this.something means "in this object" and can be assumed if it is omitted.
 
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
ok i understood that in order for "this" being called it must be used inside non-static calls that's correct because "this" is not available in non-static methods.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic