The course is part of this learning path
In this course, we'll learn about object-oriented programming and its implementations.
- Method Overriding
- Super and This Keywords
- Final Keyword
- Abstract Classes
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there, in this lesson we will look for answers to questions such as what is a reference variable?
And how can we use the keyword this as a reference variable? First let's talk about what a reference variable is. In fact, as the name suggests, variables that point to and reference a different class or field are called reference variables. For example, when we create an object, we said that this object actually occurs in the stack and references the class in the heap in the stack and heap video. Later we could access the instance variables to find in that class by using this reference variable. If we look at the example on the slide, we have a Car class and an instance variable named speed in this class. And now the stack is empty and only the heap has a space allocated for the Car class and the speed instance. If we create an object named myCar from the car class in the main method, then the myCar object will be created in stack and will reference the car class in the heap.
Then we can access the speed instance using the reference myCar and assign it a value. I think you understood what the reference variable is. Now let's look at the this keyword as a reference variable. There can be a lot of usage of the this keyword in Java. In java the "this" keyword actually is a reference variable that refers to the current object. On the slide you can see the different usage of the "this" keyword. "This" can be used to refer current class instance variable. "This" can be used to invoke the current class method implicitly. "This" can be used to invoke the current class constructor. "This" can be passed as an argument in the method call. "This" can be passed as an argument in the constructor call. "This" can be used to return the current class instance from the method. I know it doesn't make much sense that way.
Now let's move on to Eclipse and get some practice with the this keyword. First, I will create a new class right click on the superthis package and select the new and class options. The class name can be ReferenceVariableThis and I will check the checkbox for the main method and click 'Finish'. Now let's create two instant variables of this class. String name; int age; and I will create the constructor of this class, public ReferenceVariablesThis() and for the parameter of this constructor I write string name, int age. And in the constructor I write name = name; age = age; Now, let's reach these objects from the main method using the reference object of this class. ReferenceVariableThis ref = new ReferenceVariableThis. And for the name and age parameters, I write David and 20. Now I will access the name and age variable using the ref object and print them on the console. System.out.printIn("name : " after the plus sign I write ref.name. I'll copy and paste this line once and this will be age.
Okay, yes. Do you think I will be able to see David and 20 values on the console screen now when I run the application? Actually we won't be able to see it. Let's run it and observe the result. As you can see the name is null and the age is zero. Actually, these values are the default values of name and age variables, so there are no values assigned to these variables. So, what is the reason for this? Let's see now. If we look inside the constructor when I click on the variable name, it references the parameter name variable. When I click on the age variable, it still references the parameter age variable. In other words it does not reference the name and age variables here, which are instances of this class. This is where the this keyword comes into play. If we want the name and age variables here to reference instances of this class, we should use the this keyword at the beginning of each variable. Let's fix it now. This will be this.name and this one will be this.age.
If you notice this name which is the instance of this class. The age here also refers to age which is also an instance of this class and the name and age to the right of the equal sign refer to the variables that are parameters. If you want let's run and test the application now. As you can see we saw David and 20 values on the console screen this time. So, why do we have to use the this keyword? So, how can we do the same without using the this keyword? If the names of the parameters in the constructor and the variables that are instances are different from each other, then you can also not use the this keyword. For example, instead of the age parameter in the constructor, I write myAge and I'm removing this keyword here. Now let's run the application once again. As you can see, the result is the same. Now let me explain this.
In fact, the compiler automatically adds the this keyword during compilation, but since the instance and parameter have different names, the compiler does not confuse to whom the variable here is referencing. In short, the thing to keep in mind is that if the instance name in a class and the parameter name in the constructor are the same, then we use the this keyword to avoid confusion. The this keyword tells the compiler that this variable references the instance in the class. If the names are different, the compiler itself can distinguish them and there is no need to use this, but common usage is to use the this keyword inside the constructor. Let's look at a different usage of the this keyword. For example, there are two methods in this class that print 'Hello Java Developers' to the console. Let the first method be called printHello. This method will print only the word 'Hello'. System.out.printIn("Hello "); also, let this be the print method instead of println. Let's copy and paste this method. Let the name of the method be printJava. This method will print the expression 'Java Developers'.
I'm changing the message to 'Java Developers'. Now, I will call the second method inside the first method. I'm calling the printJava method just below the print method. In fact, this is the method calling we've done so far, but if we want, we can use the this keyword here as well. Actually, the compiler already puts it in automatically at the compiling stage. But since there is no other method with that name in this class anyway, the compiler is not confused and we don't use the this keyword for that. But I wanted you to see that there is such a use. Here the this keyword again references this class. That's what this line tells the compiler, go to the reference variables This class and call the printJava method inside that class. I think it's understood. Finally, let's call this method inside the main method; ref.PrintHello(); Now, let's test the application. As you can see, we have printed 'Hello Java Developers' on the console screen. Finally, let's look at the use of the this keyword as a constructor call. First, let's create the empty constructor of This class. public ReferenceVariablesThis();
The empty constructor does not take any parameters. Now, let's use the this keyword which acts as a call in this constructor. If you notice, I'm using parentheses after this() here. Using the this keyword in this way means calling the empty constructor of This class. If you pay attention we will get an error if we leave it like this. The error says 'Recursive constructor invocation'. In other words, we are calling the empty constructor once again inside the empty constructor and this will create an ever repeating cycle. So, the this keyword will call the empty constructor and the empty constructor will call the this call again and so on. So, if we leave it that way, we get a compiler error here. Now, let's assign values corresponding to the instances of this() class in parentheses. Let the name be Ronaldo. Let the age be 30, as such. This actually reference the full constructor here. If you press the CTRL key on the keyboard and click the this word here, it'll take you to the constructor here.
Also, if we delete the parameters here while creating the object of this class in the main method, we will no longer get an error because with this call we have already assigned values to name and age variables. If you want, let's try and test it. As you can see, we see Ronaldo and 30 values on the console screen. In short, we can also use the this keyword as the constructor call, but avoid unnecessary looping of your code when using it. For example, let's use the this keyword as the call in the second constructor. If you notice, we got a compilation error again this time because this() we write here calls the the empty constructor. That is, the first constructor. When we go to the first constructor, there is this which calls the second constructor. So, we're constantly going back and forth between the first and second constructor. This causes the compiler to throw an error. Finally, I'll talk about one more point and end the lesson. We cannot use the keyword this which acts as a call everywhere. For example, let's try to use it in the printJava method here. Notice that we got a compilation error. The error says constructor call must be the first statement in a constructor. Therefore it should only be used within constructors and before any other code in the constructor. Yes, I think you understand what a reference variable is and the different uses of the this keyword. Let's take a short break here. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.