super and this keywords in Java
super keyword is used to access methods of the parent class while this is used to access methods of the current class.
- this is a reserved keyword in java i.e, we can’t use it as an identifier.
- this is used to refer current-class’s instance as well as static members.
// Program to illustrate this keyword// is used to refer current classclassRR {// instance variableinta =10;// static variablestaticintb =20;voidGFG(){// referring current class(i.e, class RR)// instance variable(i.e, a)this.a =100;System.out.println(a);// referring current class(i.e, class RR)// static variable(i.e, b)this.b =600;System.out.println(b);}publicstaticvoidmain(String[] args){// Uncomment this and see here you get// Compile Time Error since cannot use// 'this' in static context.// this.a = 700;newRR().GFG();}}chevron_rightfilter_none100 600
- 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.
// Program to illustrate super keyword// refers super-class instanceclassParent {// instance variableinta =10;// static variablestaticintb =20;}classBaseextendsParent {voidrr(){// referring parent class(i.e, class Parent)// instance variable(i.e, a)System.out.println(super.a);// referring parent class(i.e, class Parent)// static variable(i.e, b)System.out.println(super.b);}publicstaticvoidmain(String[] args){// Uncomment this and see here you get// Compile Time Error since cannot use 'super'// in static context.// super.a = 700;newBase().rr();}}chevron_rightfilter_none10 20
Similarities in this and super
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.
// Java Program to illustrate using this // many number of times class RRR { // instance variable int a = 10; // static variable static int b = 20; void GFG() { // referring current class(i.e, class RR) // instance variable(i.e, a) this.a = 100; System.out.println(a); // referring current class(i.e, class RR) // static variable(i.e, b) this.b = 600; System.out.println(b); // referring current class(i.e, class RR) // instance variable(i.e, a) again this.a = 9000; System.out.println(a); } public static void main(String[] args) { new RRR().GFG(); } } |
100 600 9000
See above we have used this 3 times. So this can be used any number of times.
// Java Program to illustrate using super // many number of times class Parent { // instance variable int a = 36; // static variable static float x = 12.2f; } class Base extends Parent { void GFG() { // referring super class(i.e, class Parent) // instance variable(i.e, a) super.a = 1; System.out.println(a); // referring super class(i.e, class Parent) // static variable(i.e, x) super.x = 60.3f; System.out.println(x); } public static void main(String[] args) { new Base().GFG(); } } |
1 60.3
See above we have used super 2 times. So super can be used any number of times.
Note: We can use ‘this’ as well as ‘super’ any number of times but main thing is that we cannot use them inside static context.
Let us consider a tricky example of this keyword:
// Java program to illustrate // the usage of this keyword class RR { int first = 22; int second = 33; void garcia(int a, int b) { a = this.first; b = this.second; System.out.println(first); System.out.println(second); System.out.println(a); System.out.println(b); } void louis(int m, int n) { this.first = m; this.second = n; System.out.println(first); System.out.println(second); System.out.println(m); System.out.println(n); } public static void main(String[] args) { new RR().garcia(100, 200); new RR().louis(1, 2); } } |
//it is of S.O.P(first) of garcia method 22 //it is of S.O.P(second) of garcia method 33 //it is of S.O.P(a) of garcia method 22 //it is of S.O.P(b) of garcia method 33 //it is of S.O.P(first) of louis method 1 //it is of S.O.P(second) of louis method 2 //it is of S.O.P(m) of louis method 1 //it is of S.O.P(n) of louis method 2
Flow of program : First, it start from main and then we have new RR().garcia(100, 200) then flow goes to garcia method of RR class and then in that we have
a = this.first b = this.second
Here, what is happening is that the value of instance variable(i.e, first) and the value of static variable(i.e, second) are assigned to the garcia method’s local variables a and b respectively. Hence value of a and b comes out to be 22 and 33 respectively. Next we have new RR().louis(1, 2) hence here flow goes to the louis method of RR class and in that we have
this.first = m this.second = n
Here, above what happens is that the value of the louis method’s local variables i.e, m and n are assigned to the instance as well as static variables i.e, first and second respectively.
Hence, the value of first and second variables are same which we have passed into the louis method i.e, 1 and 2 respectively.
This article is contributed by Rajat Rawat. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Java.util.BitSet class methods in Java with Examples | Set 2
- Shadowing of static functions in Java
- How does default virtual behavior differ in C++ and Java ?
- How are Java objects stored in memory?
- How are parameters passed in Java?
- Are static local variables allowed in Java?
- final variables in Java
- Default constructor in Java
- Assigning values to static final variables in Java
- Comparison of Exception Handling in C++ and Java
- Does Java support goto?
- Arrays in Java
- Inheritance and constructors in Java
- More restrictive access to a derived class method in Java
- Comparison of static keyword in C++ and Java



