[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
What does the 'this' operator do in Java? I still don't
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /adv/ - Advice

Thread replies: 8
Thread images: 2
File: hqdefault (1).jpg (12 KB, 480x360) Image search: [Google]
hqdefault (1).jpg
12 KB, 480x360
What does the 'this' operator do in Java? I still don't get it.
>>
File: RuntUEC.jpg (348 KB, 1228x1478) Image search: [Google]
RuntUEC.jpg
348 KB, 1228x1478
Refers to any generic object. Think of it as the "x" in algebra
>>
>>16791656
Do you know object oriented programming?
Obviously you don't.
You might want to check in to it.
Basically you create a class with something like this
Class Faggot(){
private string name;
private int age;
private boolean cuck;


public void setName(string name){
this.name = name;
}
}

that would go in a Faggot.java file, now this "class" thing is like a blueprint for an object, and object basically is 'instance' of a class, a group of variable and functions that you use to be more organized, reduce complexity at some point and most importantly reusability.

Say that you want to create an object based on the class described before, you should use something like the following:

Faggot myObj = new Faggot();

now we have created the object myObj, basically object means a "reserved space on memory tho hold structured data". So now the myObj object has the properties that the "blueprint" Faggot represents. So embedded on it you can find the properties "name", "age","cuck".
Now as you can see I also defined the function "setName" that receives a string named "name" and assigns its value to the object property "name". When the operator "this" is used, it refers to the object that is calling the property, in this case myObj. To use this method we could use the following 'myObj.setName("OP")', that would give the property name of the object the value of "OP". Before the method was called the property "name" was null.
>>
>>16791720
yeah, I don't know anything about it, hence why I asked this question
>>
>>16791720
thx tho!
>>
>>16791730
Just look for a tutorial in youtube.
when it comes to programming you'll fine 60% of what you could need there.
>>
This should go on /g/ desu, but:

If you have a class defined as such:

class A{
String s="bye";

void q(){
String s = "hi";
System.out.println(s);
System.out.println(this.s);
}
}

It will print:
hi
bye

Why? Because 's' in q() is local to q(), and Java chooses the most local variables to use as 's'. However, when you call 'this.s', it refers specifically to the class variables. You can safely leave it out 99% of the time, but it can make things more explicit, and sometimes makes debugging easier if someone has really shitty naming conventions (as in the example I just gave).
>>
>>16791656
It refers to the class instance of something.
Thread replies: 8
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.