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?
No description
62 Replies
JavaBot
JavaBot12mo ago
This post has been reserved for your question.
Hey @applesandhoney! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
noComment | Kez
noComment | Kez12mo ago
this makes sure you refer to the instance variable.
applesandhoney
applesandhoneyOP12mo ago
But can’t the same thing be accomplished with the way I wrote instead? Or would there be errors?
noComment | Kez
noComment | Kez12mo ago
This should work
applesandhoney
applesandhoneyOP12mo ago
I’m free to criticism in my technique btw, I just want to learn the point of this So both ways work?
noComment | Kez
noComment | Kez12mo ago
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 handeling
applesandhoney
applesandhoneyOP12mo ago
But so can I also not access it just by calling it without the this?
noComment | Kez
noComment | Kez12mo ago
but yes
applesandhoney
applesandhoneyOP12mo ago
No like that’s how I’ve been writing code till now which is why I’m lost Like without the this
noComment | Kez
noComment | Kez12mo ago
since there is no way the interpreter can misinterpret what you mean the thiscall 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... standardized
applesandhoney
applesandhoneyOP12mo ago
standardized is like a general style of writing code right?
noComment | Kez
noComment | Kez12mo ago
yes
applesandhoney
applesandhoneyOP12mo ago
so basically it’s just for sophistication?
noComment | Kez
noComment | Kez12mo ago
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 constructor
applesandhoney
applesandhoneyOP12mo ago
that makes sense
noComment | Kez
noComment | Kez12mo ago
the this keyword in quite useful in general
applesandhoney
applesandhoneyOP12mo ago
waittt so that gets a reference of the method then?!
noComment | Kez
noComment | Kez12mo ago
its handy to have something that can point to "itself"
public class MyClass {
// Instance variable
private String name;

// Constructor
public MyClass(String name) {
this.name = name;
}

// Method to print name
public void printName() {
System.out.println("Name: " + this.name);
}

// Method to call another method using 'this'
public void callPrintName() {
this.printName();
}
}

public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass("John");
obj.callPrintName(); // This will print "Name: John"
}
}
public class MyClass {
// Instance variable
private String name;

// Constructor
public MyClass(String name) {
this.name = name;
}

// Method to print name
public void printName() {
System.out.println("Name: " + this.name);
}

// Method to call another method using 'this'
public void callPrintName() {
this.printName();
}
}

public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass("John");
obj.callPrintName(); // This will print "Name: John"
}
}
here are 3 use cases for this
applesandhoney
applesandhoneyOP12mo ago
thank you for sending this!
JavaBot
JavaBot12mo ago
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.
applesandhoney
applesandhoneyOP12mo ago
sry i have one more question
noComment | Kez
noComment | Kez12mo ago
Ask away
applesandhoney
applesandhoneyOP12mo ago
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?
noComment | Kez
noComment | Kez12mo ago
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?
applesandhoney
applesandhoneyOP12mo ago
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?
noComment | Kez
noComment | Kez12mo ago
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.
applesandhoney
applesandhoneyOP12mo ago
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?
noComment | Kez
noComment | Kez12mo ago
You got it backwards
applesandhoney
applesandhoneyOP12mo ago
sorry i mean that whether I use this or not oh
noComment | Kez
noComment | Kez12mo ago
The reference is like a link to the original object
applesandhoney
applesandhoneyOP12mo ago
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
noComment | Kez
noComment | Kez12mo ago
the original values inside of an object can change let me try to write a quick example
applesandhoney
applesandhoneyOP12mo ago
okay tysm
noComment | Kez
noComment | Kez12mo ago
./run
public class Main {
public static void main(String[] args) {
int num = 10;
System.out.println("Before: " + num);
changeValue(num);
System.out.println("After: " + num);
}

public static void changeValue(int num) {
num = 20;
}
}
public class Main {
public static void main(String[] args) {
int num = 10;
System.out.println("Before: " + num);
changeValue(num);
System.out.println("After: " + num);
}

public static void changeValue(int num) {
num = 20;
}
}
I Run Code
I Run Code12mo ago
Here is your java(15.0.2) output @noComment | Kez
Before: 10
After: 10
Before: 10
After: 10
noComment | Kez
noComment | Kez12mo ago
./run
public class Main {
public static void main(String[] args) {
int[] arr = {10};
System.out.println("Before: " + arr[0]);
changeValue(arr);
System.out.println("After: " + arr[0]);
}

public static void changeValue(int[] arr) {
arr[0] = 20;
}
}
public class Main {
public static void main(String[] args) {
int[] arr = {10};
System.out.println("Before: " + arr[0]);
changeValue(arr);
System.out.println("After: " + arr[0]);
}

public static void changeValue(int[] arr) {
arr[0] = 20;
}
}
I Run Code
I Run Code12mo ago
Here is your java(15.0.2) output @noComment | Kez
Before: 10
After: 20
Before: 10
After: 20
noComment | Kez
noComment | Kez12mo ago
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
public class Main {
public static void main(String[] args) {
int[] arr = {10};
System.out.println(arr);
}
}
public class Main {
public static void main(String[] args) {
int[] arr = {10};
System.out.println(arr);
}
}
I Run Code
I Run Code12mo ago
Here is your java(15.0.2) output @noComment | Kez
[I@4f51b3e0
[I@4f51b3e0
noComment | Kez
noComment | Kez12mo ago
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.
applesandhoney
applesandhoneyOP12mo ago
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?
noComment | Kez
noComment | Kez12mo ago
what? what do you mean?
applesandhoney
applesandhoneyOP12mo ago
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?
noComment | Kez
noComment | Kez12mo ago
it is. It is literally just passing the content of the variable to the method, not the reference to the variable.
applesandhoney
applesandhoneyOP12mo ago
okay
applesandhoney
applesandhoneyOP12mo ago
No description
applesandhoney
applesandhoneyOP12mo ago
in the part where it says num = n, that’s assigning n to num’s reference?
noComment | Kez
noComment | Kez12mo ago
no here you are accessing a static member of a class and change that directly this has an implicit thisagain this.num = n; num doesn't have a reference (i think) since it is a primitive type
applesandhoney
applesandhoneyOP12mo ago
implicit as in it just happens without me having to code it?
noComment | Kez
noComment | Kez12mo ago
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
applesandhoney
applesandhoneyOP12mo ago
sorry im being so repetitive but this just gets the reference of that variable right?
noComment | Kez
noComment | Kez12mo ago
on the heap there can also be references to the stack there is NO REFERENCE for primitive types
applesandhoney
applesandhoneyOP12mo ago
but then how does ‘this’ work then because it gets the reference?
noComment | Kez
noComment | Kez12mo ago
we dont need a reference since we are accessing the varbiable itself. it is a member of the class.
applesandhoney
applesandhoneyOP12mo ago
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
noComment | Kez
noComment | Kez12mo ago
thisis referencing the current instance of the class Main, so the object we are dealing with at the moment
applesandhoney
applesandhoneyOP12mo ago
okay it’s making sense again now hope it stays like that in my brain
noComment | Kez
noComment | Kez12mo ago
hope so too 🙂
applesandhoney
applesandhoneyOP12mo ago
thank you very much though!
JavaBot
JavaBot12mo ago
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.
JavaBot
JavaBot12mo ago
Post Closed
This post has been closed by <@1127841130252357733>.
Want results from more Discord servers?
Add your server