The discussion focuses on the use of the 'this' keyword in Java, specifically within the context of constructors in the Box class. The 'this' keyword is used to differentiate between instance variables and parameters with the same name, ensuring clarity in code. An example is provided where the constructor initializes width, height, and depth using 'this' to refer to the instance variables. The explanation clarifies that an object is an instance of a class, while instance variables are unique to each object created from that class.
PREREQUISITES
Understanding of Java programming language
Familiarity with object-oriented programming concepts
Knowledge of class and object definitions in Java
Basic understanding of constructors in Java
NEXT STEPS
Study Java constructors and overloading techniques
Learn about instance variables and their scope in Java
Explore the use of the 'super' keyword in Java inheritance
Review Java documentation on the 'this' keyword for advanced usage
USEFUL FOR
Java developers, software engineers, and students learning object-oriented programming concepts will benefit from this discussion on the 'this' keyword and its application in constructors.
#1
pairofstrings
411
7
Hello everyone. I want explanation of the working of 'this' keyword in java. I read articles but couldn't understand. Please help me here. I don't understand what objects and variables they are talking about. Also give me an example of what object, variable, instance variable is.
Writing code like that is a common convention especially for constructor functions.
Actually your example is a bit silly. A better example would be
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
In that example there are two different variables called "width". One of them will have been declared in the class. The other one is the parameter to the constructor.
Within the constructor, the parameters are local variables and therefore the parameter "width" "hides" the class variable with the same name. "width" refers to the parameter, and "this.width" refers to the class variable. "this" just means "the name of the object of type Box that this function call refers to".
Your original example would work just as well without using "this":
because all the variable names are unique anyway. But using "this" makes it clear which variables belong to the class and which are local to the function.
Last edited:
#3
Adyssa
202
3
An object is an instance of a class. A variable is a general term for a named "bucket" that you can keep a value inside (int number, float precise_number, String mySentence - these are all variables, and they all have a different type which is important, so the computer has some idea of what kind of variable you are talking about. Words are treated differently from numbers.) An instance variable, ok so you have a class (a template), and from this class you instantiate objects (you say, computer take this class design (of a box say), and build me an object (a box). Build as many boxes as you want. Box1, Box2, Box3 - all box objects created from the Box class. Each Box object has it's own copies of the variables that make up a box (width, height, depth). If you change one, you don't affect the other Boxes, because they are instance variables, related to the particular instance (Box1, Box2, Box3) of the class (Box)
Here's a small example:
Code:
public class Box
{
private:
int width, height, depth;
public Box(int width, height, depth)
{
this.width = width;
this.height = height;
this.depth = depth;
}
int getVolume()
{
volume = width * height * depth;
return volume;
}
public static void main()
{
Box Box1 = new Box(10, 10, 10);
Box Box2 = new Box(20, 20, 20);
Box Box3 = new Box(30, 30, 30);
int Vol1 = Box1.getVolume();
int Vol2 = Box2.getVolume();
}
}
Here I declare a class called Box. It contains a constructor, which is a special function to initialize the variables when you instantiate the class. What does that mean? In the main() function, refer to the line that says "Box Box1 = new Box(10, 10, 10);" This is an example of creating an object from a class definition, or instantiating the class, or creating an instance of the class. Before this, your class is just a template (a box design). After you create an object with the keyword new you have something useable, an actual Box.
You can see that we call getVolume() on two different instances of the Box class (Box1, Box2). They each have different width, height and depth, hence they will return a different volume, because their instance variables contain different values. Same class, different object (or instance).
The keyword this is a way of referring to the current object. See this page for a pretty good explanation with an example:
You can also use it to call the basic constructor if you have multiple (overloaded) constructors. The keyword super is closely related to this, it refers to the super-class, or parent-class, you might not be up to that just yet.
Excuse my Java if I made any glaring mistakes, it's been a while. :P