Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this.xxx

 
may leung
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test {
static int total = 10;
public static void main (String args []) {
new Test();
}
public Test () {
System.out.println("In test");
System.out.println(this);// line 1
int temp = this.total;// line 2
if (temp > 5) {
System.out.println(temp);
}
}
}
In this question, the ans is 10 is one of the output.
But my question is what is the" this "in line one means? I complile the program and it outputs some numbers seem no meanings.
And for line 2, I put total instead of this.total still got the same result. In this case, Is there any different that I use this or no this ??
 
Kalpesh Soni
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem very much new to java !
this is programmer certification study forum
you can ask such questions in general java forum
i am not trying to offend anybody by saying so

still i ll try to answer your question
this is a reference to the object whose method is getting called
it means
if you say
String s = new String("hello");
s.compareTo("hi");
than inside compareTo method you can refer your string object by this keyword
meaning this and s point to same object
and this is not available outside any instance methods
nor inside static method

------------------
KS
 
sasi dhulipala
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In this question, the ans is 10 is one of the output.
This is correct.
But my question is what is the" this "in line one means?
this means the current object referring to the Test class
it means it is the reference returned by the call to new Test();
I complile the program and it outputs some numbers seem no meanings.
All the out out has a meaning attached to it
The statement System.out.println("In test"); gives rise to
out put >> In test;
the statement at line one orints the currenct reference to the object of Test on my system it give Test@1dae076f but it may differ on yr system Nothing wrong in it.
line 2 gives an out put of 10;
And for line 2, I put total instead of this.total still got the same result. In this case, Is there any different that I use this or no this ??
since total is declared as static it is a class varaible and can be approached directly from class name or through an object of it.
so all calls like
int temp = total; // being class variable to avaiable
//directly to this method
int temp = this.total; // approach from an object of class
int temp = Test.total; // approach from class name directly.
HTH
sasi
[/b]

[This message has been edited by sasi dhulipala (edited January 09, 2001).]
 
hh33
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.prinln(this);
this here is the object you just created by new Test();
The line one prints this.toString().
Obj.toString():
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic