• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this!!

 
Ash Rai
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Everybody.
This is my first time here. I am sure you'll see me here often asking lots of questions!
Here's my doubt:
I understand that using 'this' for static instance variables makes no sense becoz they are class variables. Also I read that it is illegal to use 'this' with class variables. But I have a code here that compiles and runs too!! What's wrong?? Is it just plain redundant or is it illegal?
<CODE>public class StaticTest {
static int total=15;
StaticTest (int x) {
System.out.println("In test");
this.total=x;
if (this.total> 5) {
System.out.println(this.total);
}}
public static void main (String args []) {
StaticTest obj1=new StaticTest(7);
StaticTest obj2=new StaticTest(8);
System.out.println(obj1.total);//Output,of course,is 8!!
}
}
</CODE>
 
quan zhu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has no compiling error. I think it is because of the static var is accessed inside the constructor.
One question: When could you conside the object construction is done? At the end of constructor? Is "this" valid at the beginning of the constructor?
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" means the current object. When you call a static member, you can refer it by the class name, or the object name.
 
Ash Rai
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does this mean that using 'this' for static class variables is NOT illegal? (like I used - this.total in the example)
 
Sam Zheng
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not illegal. But if you try to use this under a
static context, your compiler will cry, as in the following:
class A{
static int x;
public static void main(String[] argv){
System.out.println("compiler crying " + this.x);
}
}
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
'this' refers to the current object. So this.variable refers to a variable belonging to that object(i.e. the object is the owner of that variable). However the static variables also belong to that object, though it is not the sole owner of the static object. Hence we can refer to static object as this.bariable.
hope you get what I mean
regards
Tanveer
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
'this' refers to the current object. So this.variable refers to a variable belonging to that object(i.e. the object is the owner of that variable). However the static variables also belong to that object, though it is not the sole owner of the static object.The static variables are jointly owned by all the instances of that class. Hence we can refer to static object as this.bariable.
hope you get what I mean
regards
Tanveer
 
Ash Rai
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this is what I gather..
Consider the following code that has been included in the above example.
public void method (){
System.out.println("Total is:"+ this.total);}
1. Since this is a non static method I can refer to total as either this.total or total. Both will compile and run.
2. If method is declared as public static void method() then I CANNOT refer to total as this.total.
Thank you all..
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. But for point 2, you can't use this in any static method, not just main(). Reason is that static can be called without creating an object, so if there is no object, what does this refer to?
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic