Jump to content

Java Question that is kind of obvious...

Go to solution Solved by Tazman192,

Say for example you have a class, and you instantiate it (i.e creating an object - a class is a blueprint for an object essentially), you can use the keyword "this" to refer to fields within the object that you just created.

 

So a quick example may be

public class Car {    public int numberOfDoors = 5;    public int maxSpeed = 70;            //constructor    public Car(int a, int b) {        numberOfDoors = a;        maxSpeed = b;    }}
You can use the constructor to create an object of type car. You may have a 3 door car, so you'd simply create an object using the constructor.

 

Saying that, a neater way to write the above code would be:

public class Car {    public int numberOfDoors = 5;    public int maxSpeed = 70;            //constructor    public Car(int numberOfDoors, int maxSpeed) {        this.numberOfDoors = numberOfDoors;        this.maxSpeed = maxSpeed;    }}
Notice the use of "this". I am referring directly to the fields within the new object you create using the character. We can use the first method, but the second method is easier to follow, and disallows any ambiguity from instance and local variables (including parameters).

 

 

 

 

 

 

 

tl;dr

 

"this" keyword can be used in the following ways:

  • To get reference of an object through which that method is called within it (instance method - i.e the example above)
  • To avoid fields shadowed by a method or constructor parameter (using the same names for local and instance fields, second piece of code demonstrates this)
  • To invoke a constructor of same class.
And a few other uses which you probably won't encounter for a while.

 

Note we can't use the "this" keyword with static methods

Hey guys! So this seems like an obvious question but my teacher for computer science is really bad. What does "this.something" mean in Java. I see it all the time when people try to help me and it really pisses me off that I can't actually benefit from their help because my teacher hasn't taught me that. If someone could explain it, I would really appreciate it. Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

This offers a good explanation; preferable than me typing it all up for you.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Dot operator is used to access methods and variables within objects and classes. They are used to access instance members of an object and class members of a class.

Thus your "this.something". Although I don't know what your classmates something() would do :(.

Link to comment
Share on other sites

Link to post
Share on other sites

There's a school of thought/practice which I follow that dictates that one should avoid using it where it's not necessary. It's a subjective topic of course but my personal opinion is simply; why type extra code where it is not required or serves no use. Sure there's readability but I don't see how the use of "this" in well formed, simple and eloquent code aids that in any way.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

this.something is especially useful if you have an object of the following structure:

class Person {   private string name;   private int age;   public Person(string name, int age) {     this.name = name;     this.age = age;   }}
Link to comment
Share on other sites

Link to post
Share on other sites

To make it simple, when you add " this. " before something, it tells the IDE that you want to use the variable or method that is present in THIS class (ie: normal class, Main class, a subclass in another one, etc.). It is especially useful when you have something in the likes of:

class Thing {private int number;public Thing(int number){this.number = number;}private int someMethod(int number){int result;result = this.number + number;return result;}}

Here, this. is used to:
1. Give the value of the class parameter to the class variable "number" in the Thing constructor.

2. Add up the value of the class variable "number" (denoted by this.number) to the new variable "number" that was entered as method parameter.

 

Hope it helps!

My current build :

Hurricane Mk.I

  • CPU - Intel Core i5-4690K @ 4.6 GHz ~ 1.25V
  • Cooling - Corsair H60 2013 Edition
  • Motherboard - Gigabyte Z97MX-Gaming 5 (mATX)
  • RAM - 16 GB DDR3-1600 Avexir Core Series (Red LEDs)
  • GPU - XFX Radeon R9 Fury X @ 1100/500 MHz -24 mV
  • Case - CoolerMaster MasterCase Pro 3
  • SSD - 256 GB ADATA SX900
  • HDD - 1 TB Seagate Barracuda 7200 RPM
  • PSU - Corsair RM750 (750W 80+ Gold)
  • Display - LG 34UM67-P Ultrawide (Freesync)
  • Keyboard - CM Storm Quickfire TK (Cherry MX Red) + CM Masterkeys Pro S RGB (Cherry MX Blue)
  • Mouse - Razer DeathAdder 2013
  • Audio - Sennheiser HD598 SE with detachable mic
  • OS - Windows 10 Home Edition
  • VR - Lenovo Explorer Windows Mixed Reality Headset
  • Laptop - ASUS K501LX with i7-5500U and GTX 950M
Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

 

That is not correct. "this" refers to the object from a class and its state, methods, and functions. Also technically, it is the compiler that interprets "this", not the IDE.

Link to comment
Share on other sites

Link to post
Share on other sites

Say for example you have a class, and you instantiate it (i.e creating an object - a class is a blueprint for an object essentially), you can use the keyword "this" to refer to fields within the object that you just created.

 

So a quick example may be

public class Car {    public int numberOfDoors = 5;    public int maxSpeed = 70;            //constructor    public Car(int a, int b) {        numberOfDoors = a;        maxSpeed = b;    }}
You can use the constructor to create an object of type car. You may have a 3 door car, so you'd simply create an object using the constructor.

 

Saying that, a neater way to write the above code would be:

public class Car {    public int numberOfDoors = 5;    public int maxSpeed = 70;            //constructor    public Car(int numberOfDoors, int maxSpeed) {        this.numberOfDoors = numberOfDoors;        this.maxSpeed = maxSpeed;    }}
Notice the use of "this". I am referring directly to the fields within the new object you create using the character. We can use the first method, but the second method is easier to follow, and disallows any ambiguity from instance and local variables (including parameters).

 

 

 

 

 

 

 

tl;dr

 

"this" keyword can be used in the following ways:

  • To get reference of an object through which that method is called within it (instance method - i.e the example above)
  • To avoid fields shadowed by a method or constructor parameter (using the same names for local and instance fields, second piece of code demonstrates this)
  • To invoke a constructor of same class.
And a few other uses which you probably won't encounter for a while.

 

Note we can't use the "this" keyword with static methods

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

  • 4 weeks later...

That is not correct. "this" refers to the object from a class and its state, methods, and functions. Also technically, it is the compiler that interprets "this", not the IDE.

I wanted to make it simpler to understand, but you are indeed correct (and I did goof up on the IDE part).

My current build :

Hurricane Mk.I

  • CPU - Intel Core i5-4690K @ 4.6 GHz ~ 1.25V
  • Cooling - Corsair H60 2013 Edition
  • Motherboard - Gigabyte Z97MX-Gaming 5 (mATX)
  • RAM - 16 GB DDR3-1600 Avexir Core Series (Red LEDs)
  • GPU - XFX Radeon R9 Fury X @ 1100/500 MHz -24 mV
  • Case - CoolerMaster MasterCase Pro 3
  • SSD - 256 GB ADATA SX900
  • HDD - 1 TB Seagate Barracuda 7200 RPM
  • PSU - Corsair RM750 (750W 80+ Gold)
  • Display - LG 34UM67-P Ultrawide (Freesync)
  • Keyboard - CM Storm Quickfire TK (Cherry MX Red) + CM Masterkeys Pro S RGB (Cherry MX Blue)
  • Mouse - Razer DeathAdder 2013
  • Audio - Sennheiser HD598 SE with detachable mic
  • OS - Windows 10 Home Edition
  • VR - Lenovo Explorer Windows Mixed Reality Headset
  • Laptop - ASUS K501LX with i7-5500U and GTX 950M
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×