JAVA THIS KEYWORD
In Java programming language, 'this' keyword is used to refer the current object of a class and we can refer it to any member of class.
It means we can access any member variable and method by using this keyword.
Actually the main purpose of 'this' keyword to solve the confusion, when we have any variable name for instance and local variable.
The main purpose of using 'this' keyword is to solve the confusion when we have same variable name for instance and local variables.
The main purpose of using 'this' keyword is to solve the confusion when we have same variable name for instance and local variables.
So we can use the, this keyword for the following purposes.
1. this keyword is used to refer to current object.
2. this always a reference to the object on which method was invoked.
3. this keyword can be used to invoke current class constructor.
4. this can be passed as an argument to another method.
THIS KEYWORD EXAMPLE
class this_example
{
int roll;
String name;
float marks;
this_example(int roll, String name, float marks)
{
this.roll = roll;
this.name = name;
this.marks = marks;
}
public static void main(String[] args)
{
this_example ob = new this_example(101,"Rakesh",45);
System.out.println("\nRoll numberis: "+ob.roll);
System.out.println("Name is: "+ob.name);
System.out.println("Marks is: "+ob.marks);
}
}
Program Explanation: In the above program (this example program) we have three constructor variables (as local parameter variables) with same name of class variables.
Therefore we are using 'this' keyword to assign values to class variables.
CALLING CONSTRUCTOR BY USING THIS
class msg
{
msg()
{
// Calling constructor
this("Welcome in Enotes4 learning hub");
}
msg(String str)
{
System.out.println(str);
}
public static void main(String[] args)
{
msg ob = new msg();
}
}
Program Explanation: In the above program, the parameterized constructor calling by non-parameterized contstructor (default constructor)
Default constructor argument "Welcome in Enotes4 learning hub" passing to "str" variable of parameterized constructor and this argument printing by parameterized constructor too.
ACCESSING CLASS METHOD BY USING THIS
class msg
{
public void input()
{
System.out.println("Calling simple function by this keyword");
}
public void display()
{
this.input();
}
public static void main(String[] args)
{
msg ob = new msg();
ob.display();
}
}
THIS KEYWORD EXAMPLE
class this_example
{
int roll;
String name;
float marks;
this_example(int roll, String name, float marks)
{
this.roll = roll;
this.name = name;
this.marks = marks;
}
public static void main(String[] args)
{
this_example ob = new this_example(101,"Rakesh",45);
System.out.println("\nRoll numberis: "+ob.roll);
System.out.println("Name is: "+ob.name);
System.out.println("Marks is: "+ob.marks);
}
}
- - - PROGRAM OUTPUT - - -
Program Explanation: In the above program (this example program) we have three constructor variables (as local parameter variables) with same name of class variables.
Therefore we are using 'this' keyword to assign values to class variables.
CALLING CONSTRUCTOR BY USING THIS
class msg
{
msg()
{
// Calling constructor
this("Welcome in Enotes4 learning hub");
}
msg(String str)
{
System.out.println(str);
}
public static void main(String[] args)
{
msg ob = new msg();
}
}
- - - PROGRAM OUTPUT - - -
Program Explanation: In the above program, the parameterized constructor calling by non-parameterized contstructor (default constructor)
Default constructor argument "Welcome in Enotes4 learning hub" passing to "str" variable of parameterized constructor and this argument printing by parameterized constructor too.
ACCESSING CLASS METHOD BY USING THIS
class msg
{
public void input()
{
System.out.println("Calling simple function by this keyword");
}
public void display()
{
this.input();
}
public static void main(String[] args)
{
msg ob = new msg();
ob.display();
}
}
- - - PROGRAM OUTPUT - - -
ABSTRACT CLASS >>
0 Comments