MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java this and super keyword


this keyword in Java :


this is a way for us to reference an object of the class which is being created/referenced.
It is used to call the default constructor of the same class.
this keyword eliminates the confusion between the parameters and the class attributes with the same name. Take a look at the example given below :

class cwh{
    int x;

//    getter of x
    public  int getX(){
        return  x;
    }

    // Constructor with a parameter
    cwh(int x) {
        x = x;
    }

    // Call the constructor
    public static void main(String[] args) {
        cwh obj1 = new cwh(65);
        System.out.println(obj1.getX());

    }
}

Output

0

In the above example, the expected output is 65 because we've passed x=65 to the constructor of the cwh class. But the compiler fails to differentiate between the parameter 'x' & class attribute 'x.' Therefore, it returns 0.

Now, let's see how we can handle this situation with the help of this keyword. Take a look at the below code :
class cwh{
    int x;

//    getter of x
    public  int getX(){
        return  x;
    }

    // Constructor with a parameter
    cwh(int x) {
        x = x;
    }

    // Call the constructor
    public static void main(String[] args) {
        cwh obj1 = new cwh(65);
        System.out.println(obj1.getX());

    }
}

Output

65

Now, you can see that we've got the desired output

Super keyword


A reference variable used to refer immediate parent class object.
It can be used to refer immediate parent class instance variable.
It can be used to invoke the parent class method.

Example


package com.company; 
import javax.print.Doc; 
class EkClass{ 
int a; 
public int getA() { 
return a; 
} 
EkClass(int a){ 
this.a = a;
 } 
public int returnone(){
 return 1; 
} 
} 
class DoClass extends EkClass{ DoClass(int c){ super(c); 
System.out.println("I am a constructor"); } 
} 
public class cwh_47_this_super { 
public static void main(String[] args) {
 EkClass e = new EkClass(65); 
DoClass d = new DoClass(5); 
System.out.println(e.getA()); } }


Conclusion

In this page (written and validated by ) you learned about Java this and super keyword . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Method Overriding.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.