OCAJP – How to use this and super keywords in Java?

We are writing the series of posts on OCAJP certification exam. We are offering OCAJP 7, OCAJP 8, OCPJP 6, OCPJP 7 and OCPJP 8 for programmer level certifications. Keep watching our blog updates for the interesting tips on certification topics.

We have also written about how to prepare for OCAJP and how to prepare for OCPJP certifications. This posts are very useful if you are looking for guidance on preparing for the certification exams.

How to use this and super keywords?

This post is about the OCAJP exam objective “Use super and this to access objects and constructors“. In the exam , you will be tested about super,this keywords usage. Here we would try to cover all the points related to this exam objective. In the following sections, we explain how to use super and this keyword with examples.

What is the use of this keyword in Java?

“this” keyword is used for referring the curreynt instance of an object. If you use this keyword, that means you are referring the current context or instance of that class. This simplifies the need to create an object using the “new” keyword.

  • “this” keyword is used to access present class object instance variables.
  • “this” is mostly used in constructors while initializing instance variables if constructor parameter names are same as instance variables.
  • “this” keyword can be used in the method to access the instance variables.

Example :

public class Test {
	int num1;
	int num2;

	public Test(int num1, int num2) {
		this.num1 = num1;
		this.num2 = num2;
	}

	public static void main(String[] args) {
		Test t = new Test(7, 8);
		System.out.println(t.num1 + " " + t.num2);//it prints 7 8
	}

}
  • If constructor parameter names and instance variables are different, you are not required to use this. If you use this, there won’t be any compile time error.

Example :

public class Test {
	int num1;
	int num2;

	public Test(int n1, int n2) {
		num1 = n1;
		num2 = n2;
	}

	public static void main(String[] args) {
		Test t = new Test(7, 8);
		System.out.println(t.num1 + " " + t.num2);// it prints 7 8
	}

}
  • When super class and sub class contains same variable names,sub class variable hides the super class variable . It is variable hiding.
  • In that situation if you print that variable , JVM always print sub class variable value
  • Look at below example to understand this point

Example :

public class Test extends Test1 {
	int oca = 8;

	public void printValue() {
		System.out.println(oca);//prints 8
	}

	public static void main(String[] args) {

		Test t = new Test();
		t.printValue();
	}

}

class Test1 {
	int oca = 7;
}
  • In the above situation, you should use “this” to call present class current object instance variable and super to call super class instance variable

Example :

public class Test extends Test1 {
	int oca = 8;

	public void printValue() {
		System.out.println(this.oca + " " + super.oca);// prints 8 7
	}

	public static void main(String[] args) {

		Test t = new Test();
		t.printValue();
	}

}

class Test1 {
	int oca = 7;
}
  • You can also use “this” to call instance method from another instance method

Example :

public class Test {
	int oca = 8;

	public void printValue() {
		System.out.println(this.oca);// prints 8
	}

	public void call() {
		this.printValue();
	}

	public static void main(String[] args) {

		Test t = new Test();
		t.call();
	}

}
  • You can use super to call parent class method when that method is overridden in sub class

Example :

public class Test extends Test1 {
	int oca = 8;

	public int giveValue() {
		return oca;
	}

	public void call() {
		System.out.println(giveValue());// prints 8
		System.out.println(super.giveValue());// prints 7

	}

	public static void main(String[] args) {

		Test t = new Test();
		t.call();
	}

}

class Test1 {
	int oca = 7;

	public int giveValue() {
		return oca;
	}

}
  • You can’t use both this, super in static method. If you use you get compile time error

Example :

public class Test extends Test1 {
	int oca = 8;

	public int giveValue() {
		return oca;
	}

	public static void call() {
		System.out.println(this.giveValue());// generates compile time
												// error,this can't be used in
												// static method
		System.out.println(super.giveValue());// generates compile time
												// error,super can't be used in
												// static method

	}

	public static void main(String[] args) {

		Test t = new Test();
		t.call();
	}

}

class Test1 {
	int oca = 7;

	public int giveValue() {
		return oca;
	}

}

What is the use of this()?

  • You can use this() to call from constructor to constructor of same class
  • When you are calling constructor using this(), you need to pass corresponding arguments to this()
  • Let us see the below example to understand this point

Example :

public class Test {

	int num1;
	int num2;

	Test() {
		this(7);// line 1
	}

	Test(int num1) {
		this(num1, 8);// line 2
	}

	Test(int num1, int num2) {
		this.num1 = num1;
		this.num2 = num2;
	}

	public static void main(String[] args) {

		Test t = new Test();
		System.out.println(t.num1 + " " + t.num2);
	}

}

  • At line 1 , you are calling one argument constructor. so you need to pass one argument
  • At line 2 , you are calling two argument constructor. so you need to pass two arguments
  • In every constructor , the first statement should be either this() or super() (may contain arguments)
  • If you write this() after first statement of constructor, you will get compile time error

Example :

public class Test {

	int num1;
	int num2;

	Test() {
		this.num1=9;
		this(7,8);// line 1
	}
	Test(int num1, int num2) {
		this.num1 = num1;
		this.num2 = num2;
	}

	public static void main(String[] args) {

		Test t = new Test();
		System.out.println(t.num1 + " " + t.num2);
	}

}

  • You will get compile time error at line1 because this() statement is after first statement in constructor
  • So In every constructor , the first statement should be either this() or super() (may contain arguments)

What is the use of super()?

  • You can use super() to call from one constructor of sub class to another constructor of super class
  • When you are calling super class constructor using super(), you need to pass corresponding arguments to super()
  • Let us see the below example to understand this point

Example :

public class Test extends Test1 {

	Test() {
		this(7);
	}

	Test(int num1) {
		this(num1, 8);
	}

	Test(int num1, int num2) {
		super(num1, num2);// line 1
	}

	public static void main(String[] args) {

		Test t = new Test();
		System.out.println(t.num1 + " " + t.num2);
	}
}

class Test1 {
	int num1;
	int num2;

	Test1(int num1, int num2) {
		this.num1 = num1;
		this.num2 = num2;
	}

}
  • At line 1 , you are calling two argument constructor of super class (Test1). so you need to pass two arguments
  • In every constructor , the first statement should be either this() or super() (may contain arguments)
  • If you write super() after first statement of constructor, you will get compile time error

Example :

public class Test extends Test1 {

	Test() {
		this(7);
	}

	Test(int num1) {
		this(num1, 8);
	}

	Test(int num1, int num2) {
	System.out.println("ocajp");
		super(num1, num2);// line 1
	}

	public static void main(String[] args) {

		Test t = new Test();
		System.out.println(t.num1 + " " + t.num2);
	}
}

class Test1 {
	int num1;
	int num2;

	Test1(int num1, int num2) {
		this.num1 = num1;
		this.num2 = num2;
	}

}
  • You will get compile time error at line1 because super() statement is after first statement in constructor
  • So In every constructor , the first statement should be either this() or super() (may contain arguments)
  • In every constructor , the first statement should be either this() or super() (may contain arguments)
  • If you didn’t type anything either this() or super(), Java compiler will insert super() statement with no arguments

Example :

public class Test extends Test1 {

	public static void main(String[] args) {

		Test t = new Test();

	}
}

class Test1 {

	Test1() {
		System.out.println("super");
	}

}

It prints

super

Summary

It is important to remember the this and super keyword rules for OCAJP and OCPJP certification exams. This article have provided the example programs to use the various scenarios. You have to practice yourself to understand the concepts and remember all the concepts for answering the exam questions.

Practice Questions

It is important to practice more number of questions for preparing for the OCAJP certification exam. We have prepared 600+ high quality questions that covers all the exam objectives and provides explanation for all the option given for the question. This would help you to improve our confidence on the exam before you are taking the real exam.

Technical Support

If you are looking for any technical support like more explanation on each questions or clarifications, we have a dedicated support for Java certification to answer all your queries. You can drop us a mail at info@whizlabs.com with your queries. We would respond to your questions within 12 hours of time.

Good luck for your exam preparation!!

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top