This keyword in Java
This keyword in Java
- ”this” is a java predefined keyword which is available in java.
- ”this” keyword applicable
Varaiable Level
Example:
| this.variablename; |
Method Level
Example:
| this.methodname(); |
To call the super class constructor and parameterised constructor
Example:
|
this();//need to create the constructor mandatory.this(int a,int b); //need to create the super class parameterised constructor mandatory. |
Example Program:
class Employee{
public int emp_Id=123;
public Employee(){
System.out.println("Default Constructor In Java");
}
public Employee(int id){
System.out.println("Parameterised Constructor In Java"+id);
}
public void display(){
System.out.println("This is display method");
}
}
class EmpInfo extends Employee{
public EmpInfo(){
this();//to call the super class constructorin sub class
int number=this.emp_Id;//to call the super class variable
System.out.println("The Value Was"+number);
this(12,34);
}
public void test(){
this.display();
}
public static void main(String args[]){
EmpInfo emp=new EmpInfo();
EmpInfo emp1=new EmpInfo(45);
emp.display();
}
}//class
|
Output:
|
Default Constructor In Java. Parameterised Constructor In Java 45. The Value Was:123 This is displaymethod. |
+1 201-949-7520
+91-9707 240 250