In this course, we will learn the concepts of microservice and spring framework with a focus on inheritance.
Learning Objectives
- Inheritance in Java
Intended Audience
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
Prerequisites
- Basic Java knowledge
Hello there, my friends. 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. In the stack and heap video, for example, when we created an object, we said that this object actually occurs in the stack and references the class in the heap. Later we could access the instance variables defined 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 a value to it. So, I think you now understand what a reference variable is, now let's look at the "this" keyword as a reference variable. In Java, the keyword "this" can be used in a variety of ways. The "this" keyword in java is actually a reference variable that refers to the current object. On the slide, you can see various uses of the keyword "this". "this" can be used to refer to the 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. "this" obviously makes little sense at this stage, so let's move on to Eclipse and practice using the "this" keyword.
First, I will create a new class. Right click on the superthis package and select the new and class option. The class name can be ReferenceVariableThis. And I will check the checkbox for the main method and click 'Finish'. Now let's create two instance variables of this class, String name; int age; and I will create the constructor of this class, public ReferenceVariableThis 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.println("Name: " + After the plus sign, I write ref.name. I will copy and paste this line once and this will be age.
Okay, so do you think I'll be able to see David and 20 values on the console screen now when I run the application? Nope, 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 0. So, actually these values are the default values of name and age variables, there are no values assigned to these variables. So, what is the reason for this? Let's see now. If we look inside the hen 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 "this" keyword at the beginning of each variable.
So, let's fix it now. This will be this.name and this one will be this.age. If you notice this.name now references the 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 variables that are parameters. Okay, so 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, do we have to use the "this" keyword? 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 the "this" keyword here. Now let's run the application once again, as you can see the result is the same.
Okay, so let me explain this. In fact here, the compiler automatically adds the "this" keyword during compilation, but since the instance in the parameter have different names, the compiler does not get confused about 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". However, the "this" keyword is most commonly used inside the constructor. Let's examine another way to use the word "this". 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.println("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 is what this line tells the compiler, go to the ReferenceVariableThis class and call the printJava method inside that class. All right, I think you guys understand. 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 ReferenceVariableThis, 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 references the full constructor here. If you press the 'Ctrl' key on the keyboard and click the "this" word here, it will 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 compiler error again this time because "this" we write here calls 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 want to talk about one more point and then we're almost done. 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. All right. I think you understand what a reference variable is and the different uses of the "this" keyword. So, let's take a short break here my friends and I'll 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.