*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes this.xxx Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "this.xxx" Watch "this.xxx" New topic
Author

this.xxx

may leung
Greenhorn

Joined: Nov 28, 2000
Posts: 23
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

Joined: Jan 02, 2001
Posts: 312
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


Test 094, IBM WID 6.0 cert
SCJP 1.2
SCBCD 1.3 Beta
SCWCD 1.4 Beta
SCMAD Beta
SCDJWS Beta

KS
sasi dhulipala
Ranch Hand

Joined: Dec 28, 2000
Posts: 31

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

Joined: Jan 08, 2001
Posts: 10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this.xxx
 
Similar Threads
JDCert Exam
q on testonline.com
simple question
Using this to reference static variables
John Hunt M.E Q17