Java mein this keyword ka upyog current object (vartaman object) ko refer karne ke liye kiya jata hai.
In Java, the this keyword is a reference variable that refers to the current object.
Example: Using this() to Call Another Constructor
ThisConstructorDemo.java
class Student {
Student() {
this(20); // Calls parameterized constructor
System.out.println("Default Constructor");
}
Student(int age) {
System.out.println("Age: " + age);
}
public static void main(String[] args) {
Student s1 = new Student();
}
}
Expected OutputAge: 20
Default Constructor
Java mein super keyword ka upyog parent class ke members ko access karne ke liye kiya jata hai:
In Java, the super keyword is used to access members of the immediate parent class:
- 1. Variable: Parent class ke variable ko access karna (
super.number)Access parent class variable (super.number)
- 2. Method: Parent class ke method ko call karna (
super.display())Call parent class method (super.display())
- 3. Constructor: Parent class ke constructor ko call karna (
super())Invoke parent class constructor (super())
Ex 1: Parent Variable
SuperVariable.java
class Parent {
int number = 10;
}
class Child extends Parent {
int number = 20;
void display() {
System.out.println(number);
System.out.println(super.number);
}
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}
Output20
10
Ex 2: Parent Method
SuperMethod.java
class Parent {
void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
void display() {
System.out.println("Child");
super.display();
}
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}
OutputChild
Parent
Ex 3: Parent Constructor
SuperConstructor.java
class Parent {
Parent() {
System.out.println("Parent");
}
}
class Child extends Parent {
Child() {
super();
System.out.println("Child");
}
public static void main(String[] args) {
Child obj = new Child();
}
}
OutputParent
Child
Java mein static keyword ka upyog aise members banane ke liye kiya jata hai jo individual object ke bajay class se sambandhit hote hain.
The static keyword creates members that belong to the class itself rather than individual objects.
Ex 1: Static Variable
StaticVar.java
class Student {
static String college = "BIKAS";
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
System.out.println(Student.college);
System.out.println(s1.college);
System.out.println(s2.college);
}
}
OutputBIKAS
BIKAS
BIKAS
Ex 2: Static Method
StaticMethod.java
class Test {
static void display() {
System.out.println("Static method");
}
public static void main(String[] args) {
Test.display();
}
}
OutputStatic method
Ex 3: Static Initialization Block
StaticBlock.java
class Student {
static int age;
// Static initialization block
static {
age = 20;
System.out.println("Static block");
}
public static void main(String[] args) {
System.out.println("main");
System.out.println("Age = " + age);
}
}
OutputStatic block
main
Age = 20
Ex 4: Static Nested Class
StaticNested.java
class Outer {
static int number = 100;
// Static nested class
static class Inner {
void display() {
System.out.println(number);
}
}
public static void main(String[] args) {
Outer.Inner obj = new Outer.Inner();
obj.display();
}
}
Output100
Java mein final keyword ka upyog restrictions lagane ke liye kiya jata hai:
1) final variable, 2) final method, 3) final class.
The final keyword in Java is used to apply restrictions on variables, methods, and classes.
Ex 1: final Variable
final variable ko ek baar initialize karne ke baad dobara assign nahi kiya ja sakta!
Once initialized, a final variable cannot be reassigned.
FinalVariable.java
class Test {
public static void main(String[] args) {
final int x = 10;
System.out.println(x);
// x = 20; // Compilation error!
}
}
Output10
Ex 2: final Method & final Class
final method ko subclass mein override nahi kiya ja sakta, aur final class ko extend nahi kiya ja sakta!
A final method cannot be overridden in a subclass, and a final class cannot be extended.
FinalMethodClass.java
class Parent {
final void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
// void display() {} // Compilation error!
}
final class ImmutableBase {
// Cannot be extended by any subclass
}