Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

\what is "this" mean ?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i read in a tutrial abut the "this" please tell
me if i get it right. the "this" code mean return
the vaulue of the class where the "this" refar to
like this ex if i will call upon the class i will get 45
class guy {
int a;
in b;
a = 45
b = 23
return this.a
----
class avi{
avi t = new avi();
t.guy // or should i wirte t.guy.a ??
}
this means if i call on the guy class
i will get 35 auto
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well. . . . I don't know where to start.
First of all the t.guy syntax that you are using would only be correct if there were a method in the avi class named guy(). You cannot invoke a CLASS on anything, and in your example the guy class is completely unrelated to the avi class.
Then, the guy class has not return type, but you do a return anyway. Perhaps you were thinking of having a method in the guy class to return something??? Or perhaps you meant to have a main method in the class???
Then, when you return this.a you are using the term "this" to distinguish it (normally) from it's super, but guy has no super so it is un-needed.
If you want to be able to say guy.a and have it equal 45 then you need to make the variable "a" a static (class level) variable which of course means that there IS NO "this.a" at all. Otherwise you need to make an instance of guy:
guy g = new guy();
and reference g.a (the "a" varible that belongs to g).
I don't know if I have cleared anything up or just made it muddier.
 
hp2500cp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first thank u for your answer :-)
but i did not yet get the idea of what the "this" mean ??sorry

Originally posted by Cindy Glass:
Well. . . . I don't know where to start.
First of all the t.guy syntax that you are using would only be correct if there were a method in the avi class named guy(). You cannot invoke a CLASS on anything, and in your example the guy class is completely unrelated to the avi class.
Then, the guy class has not return type, but you do a return anyway. Perhaps you were thinking of having a method in the guy class to return something??? Or perhaps you meant to have a main method in the class???
Then, when you return this.a you are using the term "this" to distinguish it (normally) from it's super, but guy has no super so it is un-needed.
If you want to be able to say guy.a and have it equal 45 then you need to make the variable "a" a static (class level) variable which of course means that there IS NO "this.a" at all. Otherwise you need to make an instance of guy:
guy g = new guy();
and reference g.a (the "a" varible that belongs to g).
I don't know if I have cleared anything up or just made it muddier.


 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
to put simply this is a inbuilt reference (pointer) to the current object...
Inside a method in a class if you use this.<variable>, u can use the value of the current object's variable value.
Madhesh
 
hp2500cp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wait a sec, as we all know - in java averything is apointer.
so whay should i care to point it if they all r pionters?
when i say a=45 i mean thta a become 45 or dont i ? :-)
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not to be a stickler - but NOTHING in java is a pointer. Object variables use references (not to be confused with pointers), but variables that are primitives hold the actual value not a reference value.
If you have many objects created, and all of them have their own primitive variable "a", and you need to clarify which objects "a" that you are looking at you use "<object variable name>.a" (this is typically used in a main() method or something).
If you are coding a class (as opposed to using a class) and you want to do something to the "a" of whatever object gets created using the class then you use the "this.a" syntax.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"this" is used to reference the current object. It's used a lot in setter methods. For example,

If you didn't use the "this" reference inside the setName() method, and use just Name = Name; then you would get null for the Name reference variable.
-Peter
Ps. Thanks to Morgan for correcting my code. I was missing the return type on my setName() method.
[This message has been edited by Peter Tran (edited January 03, 2001).]
[This message has been edited by Peter Tran (edited January 03, 2001).]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,
I wish to correct method setName should have void as return type
class Person {
private String Name;
public void setName(String Name)
{
this.Name = Name;
}
}
Thanks..
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"hp2500cp",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Thanks.
 
hp2500cp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can u give me an exmple plase

Originally posted by Cindy Glass:
Not to be a stickler - but NOTHING in java is a pointer. Object variables use references (not to be confused with pointers), but variables that are primitives hold the actual value not a reference value.
If you have many objects created, and all of them have their own primitive variable "a", and you need to clarify which objects "a" that you are looking at you use "<object variable name>.a" (this is typically used in a main() method or something).
If you are coding a class (as opposed to using a class) and you want to do something to the "a" of whatever object gets created using the class then you use the "this.a" syntax.


 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
During the constructor of the class (which is invoked each time a new instance of this class is created,
class test {
String a;
test(String a){
// a = a; using this syntax would set the METHODS a (a local variable) to the string, but the object's a is still null.
this.a = a; //using this syntax the compiler realized that you want to set the objects a to the value of the methods a.
}
public static void main(String args[]){
// *** this is the place where you actually USE the stuff that we coded up there. Of course this main might as easily have been in a different file or class - but I'm lazy. ***
test firstObject = new test(args[0]); // you have to create an object before you can reference it
test secondObject = new test(args[1]);
System.out.println("firstObject's a has a value of " + firstObject.a); // now I can "ask" the object what is the value of it's a
System.out.println("secondObject's a has a value of " + secondObject.a);
}
}
From the prompt I key in -
java test cindy glass
and I get back
firstObject's a has a value of cindy
secondObject's a has a value of glass
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I typically use "this" in constructors

and when passing the current object to another one

Are these the kind of examples you were looking for?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic