what’s the point of the this. keyword in Java?
So I’m learning Java currently and I just learned about ‘this’ but I don’t quite get what the point of it is..
From what I read… it’s used by coders in order to not confuse the instance variables with their methods’ parameter variables and hence they use ‘this’
I also read that it is to take a reference of the instance variable.
However what I don’t get it that can’t you just either use different parameter variable names in order to avoid the confusion? And why can’t I just write the instance variables name directly instead of using ‘this’, like it still works that way…?
I also attached a picture on what I mean. In the picture, there is already a different parameter variable name, so there’s no confusion in that, and when it comes to initializing the instance variable, can’t I just do
name = theName;
Or
return name;
Instead of whatever they have in the picture?
62 Replies
⌛
This post has been reserved for your question.
Hey @applesandhoney! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
this
makes sure you refer to the instance variable.But can’t the same thing be accomplished with the way I wrote instead? Or would there be errors?
This should work
I’m free to criticism in my technique btw, I just want to learn the point of
this
So both ways work?We don’t only use the keyword
this
in the context of constructors
It is a general tool to access the instance of the object we are handelingBut so can I also not access it just by calling it without the
this
?but yes
No like that’s how I’ve been writing code till now which is why I’m lost
Like without the
this
since there is no way the interpreter can misinterpret what you mean the
this
call will be implicit
it is possible to write it like you did
but its not a good practice since we like our standard functions to be... standardizedstandardized is like a general style of writing code right?
yes
so basically it’s just for sophistication?
we write getters and setters and constructors as standardized as possible so we see if they are right on the first glance even if its not our code
not our code can also be our code from like a few weeks back 😄
we can also use this for other stuff by the way
you can use
this
to call a method within the current class for example
or use it to call another constructor from a constructorthat makes sense
the this keyword in quite useful in general
waittt so that gets a reference of the method then?!
its handy to have something that can point to "itself"
here are 3 use cases for
this
thank you for sending this!
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
sry i have one more question
Ask away
basically a reference of something doesn’t access that actual something right?
so if im using
this
for a method or variable, does that mean i am not accessing the actual method or variable?You are
Java is a bit weird in the way it implements it
Do you know what pass by reference and pass by value means?
my teacher mentioned it but we haven’t learned it yet
so whether i do it by reference or actual, i still access the same method/variable?
See, Java is pass by value, for primitive types like int or char that means only the value of a variable int is passed into a method in a method call. So changing that value doesn’t affect the original variable.
It’s different for objects tho.
The value that is passed in case of an object is the reference to that object.
So if I change the value of a complex type instead the value would also change for the original variable
What do you mean by “by reference or actual”
this is always a reference to that object, not the value inside that object.
so for primitive the original variables can change and for object they can change but they hold the reference, so the original variable is still the same?
You got it backwards
sorry i mean that whether I use
this
or not
ohThe reference is like a link to the original object
so is it
primitive variable:- original variable cannot change
object variable:- original variable still doesn’t change but the object uses a reference of the original variable
the original values inside of an object can change let me try to write a quick example
okay tysm
./run
Here is your java(15.0.2) output @noComment | Kez
./run
Here is your java(15.0.2) output @noComment | Kez
Here you can see the two cases
first we see what happens to a primitive value passed into a method
the value of num stays the same before and after we changed "num" in the changeValue method
the value of the array however does change because not the value of the array is passed to the method
but the reference to that array
./run
Here is your java(15.0.2) output @noComment | Kez
here we see what is the content of the variable
arr
it is not the content of the array but the reference to the array. that is what that random stuff means.but here it’s instead passing
10
that is in num
but not the variable num
to which we could assign 20
so how would it change num
from 10
to 20
?what?
what do you mean?
like yk in
changeValue(num)
, wouldn’t it be passing 10 instead?
since num = 10, when we do changeValue(num), it’s passing 10 to changeValue right?it is. It is literally just passing the content of the variable to the method, not the reference to the variable.
okay
in the part where it says num = n, that’s assigning n to num’s reference?
no
here you are accessing a static member of a class and change that directly
this has an implicit
this
again
this.num = n;
num doesn't have a reference (i think) since it is a primitive typeimplicit as in it just happens without me having to code it?
yes
there are two types of storage in java
heap and stack
on the heap there are your primitive types like ints and all that
on the stack are your complex types so everything that is derived from object
sorry im being so repetitive but this just gets the reference of that variable right?
on the heap there can also be references to the stack
there is NO REFERENCE for primitive types
but then how does ‘this’ work then because it gets the reference?
we dont need a reference since we are accessing the varbiable itself. it is a member of the class.
so it depends on whether ‘this’ is accessing the variable itself or the reference?
also im so sorry it’s like im getting it then I don’t get it then I get it then I don’t
this
is referencing the current instance of the class Main
, so the object we are dealing with at the momentokay it’s making sense again now
hope it stays like that in my brain
hope so too
🙂
thank you very much though!
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@1127841130252357733>.