In java this is a reference variable that refers to the current object.

Usage of this keyword in java:

1. This keyword can be used to refer to a current class instance variable.

– it means if there is ambiguity between the instance variable and parameter, this keyword resolves the problem of ambiguity.

2. this() can be used to invoke the current class constructor. constructor in java

– now what this means is that this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor.

3. this keyword can be used to invoke current class methods(implicitly).

– It means we can invoke the method of the current class by using this keyword. If you don’t use this keyword, the compiler automatically adds this keyword while invoking the method.

4. this can be passed as an argument in the method call.

– Here it means that this keyword can also be passed as an argument in the method.

5. this can be passed as an argument in the constructor call.

– what means here is we can pass this keyword in the constructor also. It is useful if we have to use one object in multiple classes.

6. this keyword can also be used to return the current class instance.

– this means we can return this keyword as a statement from the method. In such a case, the return type of the method must be the class type (which is a non-primitive type).

Watch Video:

Final Keyword: Final Variable, Method and Class

The final keyword can be applied with the variables, a final variable that has no value is called a blank final variable or uninitialized final variable.

The blank final variable can be static also which will be initialized in the static block only.

Example: Java final variable:

public class FinalDemo{

final int speed = 80; // this is the final variable

void run(){

speed = 100; // The value of the speed will be 80 it will not be changed.

}

}

Example: Java final variable:

class Bike{

final void run(){ // this is a final method

}

class Bullet extends Bike{

void run(){ // So here we can’t override it.

}

Example: Java final variable:

final class Facebook{

}

class Abc extends Facebook {

// It gives the compilation error…

}

So, this is how we use the final keyword in java programming language…

Java Super Keyword:

1. super can be used to refer to the immediate parent class instance variable.

– What this means is we can use super keyword to access the data member or field of the parent class. It is used if parent class and child class have the same fields. Example:

class Animal{

String color = “white”;

}

class Dog extends Animal{

String color = “black”;

void printColor(){

System.out.println(color); // this will print the color of Dog class. and if we write the same like-

System.out.println(super.color); // this will print the color of Animal class.

}

}

class TestSuper1{

public static void main(String[] args){
Dog d = new Dog();

d.printColor();

}}

When we run the code, we can see the output as black and next is white.

2. the second point is super can be used to invoke immediate parent class method’

– It means the super keyword can also be used to invoke the parent class method. It should be used if the subclass contains the same method as the parent class. In other words, it is used if the method is overridden. Example:

class Animal{

void eat(){

System.out.println(“eating…”);}

}

class Dog extends Animal{

void eat(){

System.out.println(“eating bread…”);}

void bark(){

System.out.println(“barking…”);}

void work(){

super.eat();

bark();

}

}

class TestSuper2{

public static void main(String[] args){
Dog d = new Dog();

d.work();

}}

When we run the code, we can see the output as eating and next is barking. // In the above example Animal and Dog both classes have the eat() method, if we call eat() method from Dog class, it will call the eat() method of Dog class by default because priority is given to local. To call the parent class method, we need to use super keyword.

3. The third point is super() can be used to invoke immediate parent class constructor.

– what means from here is the super keyword can also be used to invoke the parent class constructor. Let’s see an example:

class Animal {

Animal(){

System.out.println(“Animal is created “);

}

class Dog extends Animal{

Dog(){

super();

System.out.println(“dog is created”);}

}

class TestSuper3{

public static void main(String[] args){
Dog d = new Dog();

}}

When we run the code, we can see the output as the animal is created  and the dog is created. // the important thing here is super() is added in each class constructor automatically by the compiler if there is no super() or this()….

Connect with us: