Back to Java Course

Java Course

I. This keyword

  1. this is to refer to the current instance variable of the class.

For example,

class Sample{ int a; Sample(int a){ this.a=a; } void display(){ System.out.println("a = "+a); } } class Test{ public static void main(String args[]){ Sample s1=new Sample(10); s1.display(); } }

Output

a = 10
  1. this: to call the method of the current class.

The this keyword can be used to call a method in the current class. If you don't use this keyword, the compiler will add it for you when you call the method.

For example,

class Sample{ void fun() { System.out.println("hello there"); } void foo(){ System.out.println("hello foo"); this.fun(); } } class Test{ public static void main(String args[]){ Sample s = new Sample(); s.foo(); } }

Output

hello foo hello there
  1. this() is used to call the constructor of the current class.

For example,

class Sample{ Sample() { System.out.println("In default constructor"); } Sample(int a) { this(); System.out.println("In Parameterised constructor"); } } class Test{ public static void main(String args[]){ Sample s = new Sample(10); } }

Output

In default constructor In Parameterised constructor
  1. this: to use as a parameter in a method.
class Sample{ void fun(Sample obj) { System.out.println("Received the object"); } void foo(){ fun(this); } } class Test{ public static void main(String args[]){ Sample s = new Sample(); s.foo(); } }

Output

Received the object
  1. this keyword can be used to get the current instance of a class.
class Sample{ Sample getObject() { return this; } void foo(){ System.out.println("Inside foo function") } } class Test{ public static void main(String args[]){ Sample s = new Sample(); s.getObject().foo(); } }

Output

Inside foo function

II. Final keyword

Final Variable : Variable can't be reassigned, generally used for creation of constant variables.

final int temp = 10;

Final Methods : Prevent Method Overriding

class Sample { final void fun() { System.out.println("Inside fun function"); } } class Test extends Sample { void fun() // COMPILE-ERROR! { //code } }

Final Classes : Prevent Inheritance

final class Sample { // code } class Test extends Sample // COMPILE-ERROR! { // code }

Note

If your class is declared as final it is 𝐫𝐞𝐝𝐮𝐧𝐝𝐚𝐧𝐭 to mark your methods final as well, because if your class can't be extended by other classes, that means your methods can't be overridden.

Be aware that if you mark a mutable variable as 𝐟𝐢𝐧𝐚𝐥, it means that you can't change the reference of that variable, but you can still change the state of that variable.

III. Static keyword

Static Variable

  • When a variable is declared as a static, then a single copy of the variable is created and shared among all objects at class level.
  • The static variable can be used to refer to the common property of all the objects.
  • We can say static variables are global variables.
  • The static variables get memory only once in the class that is at the time of class loading.

For example,

class Test { // static variable static int x = fun(); // static block static { System.out.println("Inside static block"); } // static method static int fun() { System.out.println("Inside fun function"); return 10; } public static void main(String[] args) { System.out.println("Value of x : " + x); System.out.println("Inside main method"); } }

Output

Inside fun function Inside static block Value of x : 10 Inside main method

Static Block

  • It is executed before the main method at the time of loading of class only.
  • If we want to perform any operation before the main method then we can put that in static block.
  • Static block is basically used to initialize the static data member.

For example,

class Test { static int x = 25; static int y; // static block static { System.out.println("Inside Static block."); y = x * 4; } public static void main(String[] args) { System.out.println("Value of x : " + x); System.out.println("Value of y : " + y); } }

Output

Inside Static block. Value of x : 25 Value of y : 100

Static Method

  • A static method can be invoked without the need for creating an instance of a class.
  • A static method can access static data member and can change the value of it.
  • A static method belongs to the class rather than the object of a class.

For example,

class Test { static int x = 1; // static variable int y = 20; // instance variable static void fun() // static method { x = 20; System.out.println("Inside fun function"); y = 10; // compilation error since there is a static reference to the non-static field b foo(); // compilation error since there is a static reference to the non-static method foo() System.out.println(super.a); // compilation error since we cannot use super in a static context } void foo() // instance method { System.out.println("Inside foo function"); } public static void main(String[] args) { // main method } }

Output

The above code results in a compilation error. There are 3 reasons for it. We are accessing an instance variable inside a static method and changing its value. We are invoking an instance method inside a static method. We are trying to make use of the super keyword inside a static method.

Static Classes

When a class is declared with the static keyword, it is said to be a static class. A class can be made static in Java if and only if it is nested inside another class. We can't use the static modifier on top-level classes, but we can use it on nested classes.

For example,

public class Test { private static String str = "Praveen Oruganti"; // Static class static class Sample { public void display(){ System.out.println(str); } } public static void main(String args[]) { Test.Sample obj = new Test.Sample(); obj.display(); } }

Output

Praveen Oruganti

Why is Java main method static?

  • It is because the object is not required to call the static method.
  • Suppose, the main method is non static, then every time we need to create the object of the class and then only can call the main method and it will lead to problem of extra memory allocation.

IV. Super keyword

In Java, the super keyword is a reference variable that refers to parent class instances. With the concept of Inheritance, the term super entered the picture.

  1. Using super with variables

When a derived class and a base class have identical data members, there is uncertainty for the JVM as to which class's data member is being referred to. In order to resolve this ambiguity, we use the super keyword with the data member's name.

For example,

class Sample1 { int x = 1; } class Sample2 extends Sample1 { int x = 2; void display() { System.out.println("The value of x is : " + super.x); } } class Test { public static void main(String[] args) { Sample2 s = new Sample2(); s.display(); } }

Output

The value of x is : 1
  1. Using super with methods

To resolve ambiguity when a parent and child class have the same-named methods, we employ the super keyword.

class Sample1 { void fun() { System.out.println("Inside Sample1's fun method"); } } class Sample2 extends Sample1 { void fun() { System.out.println("Inside Sample2's fun method"); } void display() { super.fun(); } } class Test { public static void main(String[] args) { Sample2 s = new Sample2(); s.display(); } }

Output

Inside Sample1's fun method

Let's see another example,

class Sample1 { Sample1() { System.out.println("Inside Sample1's default constructor."); } } class Sample2 extends Sample1 { Sample2() { super(); System.out.println("Inside Sample2's default constructor."); } } class Test { public static void main(String[] args) { Sample2 s = new Sample2(); } }

Output

Inside Sample1's default constructor. Inside Sample2's default constructor.