• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

this.xxx

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic