Ask coding questions

← Back to all posts
What does this. do in Java/JavaScript?
SwaroopBappanad

Recently today I asked how to use processing with HTML, and you guys helped a lot. But now, I have another question. I have seen code like this:

I am seriously confused on what (this.) does, and processing.org did not help at all. Can somebody explain?

Thanks, and have a great day!

Answered by EpicGamer007 [earned 5 cycles]
View Answer
Voters
SwaroopBappanad
Comments
hotnewtop
EpicGamer007

this refers to this instance of an object in java. For example, if you say, extended JFrame and wanted to call it's methods, you could do something like setVisible(true); but that is not a good practice. this can be used to call its methods and is much neater like this: this.setVisible(true);.

Hope this helps! :)

It removes ambiguity. According to @Coder100

Shoot, idk what ambigutiy means, i probably used that word incorrectly help

Coder100

more like remove ambiguity, but I like your definition too @AbhayBhat

EpicGamer007

@Coder100, ok, ill add that!

Coder100
LohithS
firefish

Like self in python.

Coder100
Coder100

Also btw it's this the dot (.) is used to access a method

Coder100

Ok, so basically this is a Java thing, you will need to learn more java.

Anyways, it refers to a variable defined inside a class instead of the current variable:

SwaroopBappanad

@Coder100 So would this be kind of the like the global function in python?

Coder100
Coder100
Coder100

it's the same as python's self keyword though @SwaroopBappanad

SwaroopBappanad
Coder100
Coder100

thats java