top of page

Important keywords in Java




📙 This Keyword


In Java, 'this' is a keyword that is used to refer to the current instance of a class. It can be used to access the instance variables and methods of the current object.

Here are some of the main uses of the 'this' keyword in Java:

  • Accessing instance variables: The 'this' keyword can be used to refer to instance variables of a class, which helps to differentiate them from local variables that have the same name. For example:


public class MyClass {
  int num;

  public MyClass(int num) {
    this.num = num; // using 'this' to refer to the instance variable
  }
}
  • Calling constructors: The 'this' keyword can be used to call another constructor within the same class. This is useful when you want to reuse code from another constructor or when you want to provide default values for some of the constructor parameters. For example:


public class MyClass {
  int num;

  public MyClass() {
    this(0); // calling another constructor within the same class
  }

  public MyClass(int num) {
    this.num = num;
  }
}
  • Passing the current object as a parameter: The 'this' keyword can be used to pass the current object as a parameter to another method. This is useful when you want to invoke a method on the current object from within another method. For example:


public class MyClass {
  int num;

  public void doSomething() {
    someMethod(this); 
  // passing the current object as a parameter
  }

  public void someMethod(MyClass obj) {
    obj.num = 42; 
  // accessing the instance variable of the current object
  }
}
  • Returning the current object: The 'this' keyword can be used to return the current object from a method. This is useful when you want to chain method calls together or when you want to return the current object from a builder method. For example:


public class MyClass {
  int num;

  public MyClass setNum(int num) {
    this.num = num;
    return this; // returning the current object
  }

  public void doSomething() {
    // chaining method calls together
  this.setNum(42).setNum(43).setNum(44);
  }
}

The 'this' keyword is a powerful tool in Java that allows you to work with the current object in a variety of ways. By using 'this' to refer to instance variables, call constructors, pass the current object as a parameter, and return the current object, you can write more flexible and reusable code.


📙 This using Getter and Setter


The 'this' keyword in Java is often used with getters and setters to access and modify instance variables of a class. Here's how it works:


⚡ Getters with 'this':

A getter is a method that returns the value of an instance variable. To access the instance variable using 'this' inside the getter, you simply use the syntax 'this.variableName'. Here's an example:


public class MyClass {
  private int num;

  public int getNum() {
    return this.num; // using 'this' to access the instance variable
  }
}

⚡ Setters with 'this':

A setter is a method that sets the value of an instance variable. To set the instance variable using 'this' inside the setter, you simply use the syntax 'this.variableName = value'. Here's an example:


public class MyClass {
  private int num;

  public void setNum(int num) {
    this.num = num; // using 'this' to set the instance variable
  }
}

Using 'this' with getters and setters is particularly useful when you have local variables with the same name as instance variables. In such cases, you can use 'this' to disambiguate the instance variables from the local variables. Here's an example:


public class MyClass {
  private int num;

  public void setNum(int num) {
    this.num = num; // using 'this' to set the instance variable
  }

  public int getNum() {
    int num = 42; // a local variable with the same name as the instance variable
  return this.num; // using 'this' to access the instance variable
  }
}

In the above example, the local variable 'num' inside the getter has the same name as the instance variable 'num'. By using 'this' to access the instance variable, you ensure that the correct variable is returned by the getter.


📙 How can we overload constructor using 'this' ?


In Java, the 'this' keyword can be used to call one constructor from another constructor within the same class. This technique is known as constructor chaining or constructor overloading.

Here's an example:


public class MyClass {
  private int num1;
  private int num2;
  private String name;

  public MyClass(int num1, int num2) {
    this(num1, num2, "John"); // calling the three-argument constructor using 'this'
  }

  public MyClass(int num1, int num2, String name) {
    this.num1 = num1;
    this.num2 = num2;
    this.name = name;
  }
}

In the above example, we have two constructors: one that takes two integer arguments and another that takes three arguments (two integers and a string). The two-argument constructor uses 'this' to call the three-argument constructor, passing a default value of "John" for the name parameter. By doing this, we avoid duplicating code between the two constructors and make the class more maintainable.

Note that when using constructor chaining with 'this', the call to the other constructor must be the first statement in the calling constructor. This is because the instance variables must be initialized before any other statements can be executed.


📙 Final Keyword


In Java, the final keyword is used to indicate that a variable, method, or class cannot be changed or overridden.


⚡ Final Variables:


A final variable is a variable whose value cannot be changed once it has been assigned a value. Once a value is assigned to a final variable, it becomes a constant that can be used throughout the program. Final variables are often used to declare constants whose values remain the same throughout the program. In Java, final variables can be declared using the final keyword, for example:


final int MAX_VALUE = 100;

⚡ Final Methods:


A final method is a method that cannot be overridden in a subclass. Once a method is declared final, it cannot be modified or overridden in any subclass. Final methods are often used to ensure that a method behaves consistently across all subclasses. In Java, final methods can be declared using the final keyword, for example:


class Parent {
    public final void print() {
        System.out.println("This is a final method.");
    }
}

class Child extends Parent {
    // cannot override the print() method
}

⚡ Final Classes:


A final class is a class that cannot be extended by any subclass. Once a class is declared final, it cannot be extended or modified by any subclass. Final classes are often used to prevent inheritance or to ensure that the functionality of the class remains unchanged. In Java, final classes can be declared using the final keyword, for example:


final class MyClass {
    // cannot be extended by any subclass
}

In summary, final variables, final methods, and final classes are used to declare variables, methods, and classes that cannot be modified or extended once they have been declared. Final variables are used to declare constants, final methods are used to ensure consistent behavior across subclasses, and final classes are used to prevent inheritance or to ensure that the functionality of the class remains unchanged.


Thanks for reading, and happy coding!


Important keywords in Java - > Recursion in Java: Understanding Recursive Methods and Functions



bottom of page