Java This and Final Keywords Explained

Краткое содержание

Pixel shares a guide to the Java this and final keywords, with examples showing how this distinguishes instance variables from parameters and can call constructors or methods. It also explains how final prevents reassignment, method overriding, and class inheritance, helping the community write clearer, more predictable code.

DEV Community

Java - this & final keywords

this Keyword in Java The this keyword in Java is a reference variable that refers to the current object of a class. It is commonly used to distinguish instance variables from local variables when they have the same name. Refers to the current class object. Used to access current class instance variables and methods. Used to invoke another constructor of the same class using this(). Example `class Student { String name; int age; Student(String name, int age) {
this.name = name;
this.age = age;
}

void display() {
System.out.println(this.name);
System.out.println(this.age);
}
}public class Main { public static void main(String[] args) { Student s1 = new Student("Gowtham", 20); s1.display(); } } Output: Gowtham 20 Explanation this.name refers to the instance variable. name refers to the constructor parameter. this.age refers to the instance variable. age refers to the constructor parameter. The this keyword helps us avoid confusion when variable names are the same. Common Uses of this Keyword Referring to current class instance variables. Calling another constructor using this(). Calling a method of the current object. Passing the current object as an argument. final Keyword in Java In Java, the final keyword is used to restrict changes and make code more secure and predictable. It can be applied to variables, methods, and classes to prevent modification, overriding, or inheritance. This helps in creating constant values, stable methods, and immutable classes. 1. Final Variable A final variable cannot be reassigned after it has been initialized. Example:public class Main { public static void main(String[] args) { final int age = 20; System.out.println(age);
}
}Output: 20 Here, age is a final variable, so we cannot assign another value to it. 2. Final Method A final method cannot be overridden by a subclass. Example:class Parent { final void display() { System.out.println("This is a final method"); } } class Child extends Parent { // Cannot override display() }` public class Main { public static void main(String[] args) { Child obj = new Child(); obj.display(); } } Output: This is a final method Here, the display() method is declared as final, so the child class cannot override it. 3. Final Class A final class cannot be extended by another class. Example: final class Student { void display() { System.out.println("Hello Java!"); } } public class Main { public static void main(String[] args) { Student s1 = new Student(); s1.display(); } } Output: Hello Java! Here, Student is a final class, so another class cannot inherit from it.
Java This and Final Keywords Explained
Powered by Readybot.io

Последнее от Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …

Why a Java HPA Got Stuck at 20 Kubernetes Pods

Pixel shares how a Spring Boot pricing service autoscaled to 20 Kubernetes pods and stayed there because the JVM retained committed heap memory after traffic …