Java contains a list of keywords or reserved words which are also highlighted with different colors be it an IDE or editor in order to segregate the differences between flexible words and reserved words.
What is a Keyword?
In the Java programming language, a keyword is any one of 67 reserved words that have a predefined meaning in the language. Because of this, programmers cannot use keywords in some contexts, such as names for variables, methods, classes, or as any other identifier.
Examples of Keywords
abstract – Specifies that a class or method will be implemented later, in a subclass
boolean – A data type that can hold True and False values only
char – A data type that can hold unsigned 16-bit Unicode characters
continue – Sends control back outside a loop
Learn to code from industry experts! Enroll here
This keyword
This keyword in java in Java that is used to invoke constructor, methods, static members of the current class that cannot be used as an identifier. It is used to refer current-class’s instance as well as static members. It is a reserved keyword.
‘this’ can be used in various contexts as given below:
- to refer instance variable of current class
- to invoke or initiate current class constructor
- can be passed as an argument in the method call
- can be passed as argument in the constructor call
- can be used to return the current class instance
For Example,
class Code{
int var;
void method(int var){
this.var = var;
}
}
We have a variable ‘var’ and a parameter ‘var’ inside the method function. If we want to assign the value of parameter ‘var’ to the variable ‘var’ inside the class then we have to use the this keyword. In technical terms we are using this keyword tw refer the instance of current class. “this” is a special keyword in Java that is used to refer to the instance of the current class.
Learn Coding in your Language! Enroll Here!
super keyword
Super constructor in java that is used to invoke constructor, methods of the parent class. This is possible only when one class inherits another class
‘super’ can be used in various contexts as given below:
- super is a reserved keyword in java i.e, we can’t use it as an identifier.
- super is used to refer super-class’s instance as well as static members.
- super is also used to invoke super-class’s method or constructor.
For Example,
class A{
void methodP(){
// method
}
}
class B extends A{
void methodC(){
// method
}
}
class C extends B{
void methodGC(){
// method
}
}
In the above code snippet of multi-level inheritance class B extends class A, it implies class A is immediate parent of class B. Similarly class C extends class B and now class C has two parents i.e., class A and class B, where class B is immediate parent of class C. Using the “super” keyword, we can refer to the immediate parent class’s methods, constructor, instance variables, and many more.
Learn to code from industry experts! Enroll here
Similarities in this and super Keyword in Java
1) We can use this as well as super anywhere except static area. Example of this is already shown above where we use this as well as super inside public static void main(String[]args) hence we get Compile Time Error since cannot use them inside static area.
2) We can use this as well as super any number of times in a program.
3) “this” and “super” must be the first statement if used inside the constructor. This means we cannot call both statements in a single constructor.
Difference Between this and super Keyword in Java
“this” keyword in Java
|
“super” keyword in Java
|
1. “this” is an implicit reference variable keyword used to represent the current class.
|
1. “super” is an implicit reference variable keyword used to represent the immediate parent class.
|
2. “this” is to invoke methods of the current class.
|
2. “super” is used to invoke methods of the immediate parent class.
|
3. “this” is used to invoke a constructor of the current class.
|
3. “super” is used to invoke a constructor of the immediate parent class.
|
4. “this” refers to the instance and static variables of the current class.
|
4. “super” refers to the instance and static variables of the immediate parent class.
|
5. “this” can be used to return and pass as an argument in the context of a current class object.
|
5. “super” can be used to return and pass as an argument in the context of an immediate parent class object.
|
Entri provides video classes as well on various important topics by the excellent faculties. One will get revision modules, monthly tests based on the classes. Entri provides an excellent platform with full-length mock tests including previous year question papers. It also gives you access to clarify your doubts.