Friday, November 17, 2017

This Keyword in Java with Example

This is a keyword in Java. it may be used inside the method or constructor of the class. It(this) works as a reference to the modern object whose approach or constructor is being invoked. This keyword may be used to refer to any member of the modern object from inside an instance approach or a constructor.

Use Of This Keyword in Java

  • this will be used to refer to modern-day class example variable.
  • this can be used to invoke present-day elegance method (implicitly)
  • this() may be used to invoke contemporary magnificence constructor.
  • this will be exceeded as an argument inside the approach call.
  • this could be exceeded as an argument within the constructor name.
  • this will be used to return the modern class example from the approach.

1. This will be used to refer modern-day class example variable.
This keyword may be used to refer to contemporary magnificence instance variable
Example
class my
{  
        int rollno;  
        String name;  
        float fee;  
        my(int rollno,String name,float fee)
       {  
               this.rollno=rollno;  
               this.name=name;  
               this.fee=fee;  
       }   
        void show()
      {
               System.out.println(rollno+" "+name+" "+fee);
      }  
 }  
  
class hello
{  
        public static void main(String args[])
       {  
              my m1=new my(101,"Dhiren",1000);  
              my m2=new my(102,"Bhavesh",8000);  
              m1.show();  
              m2.show();  
      }
}  
Output:
101 Dhiren 1000
102 Bhavesh 8000

2. this can be used to invoke present-day elegance method (implicitly)
you may invoke the method of the modern elegance by way of the usage of this keyword. if you do not use this keyword, compiler routinely adds this keyword at the same time as invoking the method
Example
class my
{  
      void hello()
     {
          System.out.println("hello");
     }  
      void test()
     {  
          System.out.println("test");  
          this.hello();  
     }  
}  

class myfinal
{  
       public static void main(String args[])
      {  
           my m=new my();  
           m.test();  
      }
}  
Output:
hello
test

3. this() may be used to invoke contemporary magnificence constructor.
The this() constructor call may be used to invoke the cutting-edge class constructor. it is used to reuse the constructor.
Example:
class my
{  
    my()
   {  
          this(60);  
         System.out.println("my");  
   }
  
   my(int x)
  {  
        System.out.println(x);  
  }  
}
  
class TestThis6
{  
    public static void main(String args[])
   {  
        my m=new my();  
   }
}  
Output:
60
my

4. this will be exceeded as an argument inside the approach call.
This keyword also can be passed as an argument inside the method. it's miles in particular used in the occasion managing.
Example:
class test
{  
    void d(test obj)
    {  
             System.out.println("method invoked");  
    }
    void my()
    {  
             d(this);  
    }
    public static void main(String args[])
    {  
            test t = new test();  
            t.my();
    }   
}
Output:
method invoked 

5. this could be exceeded as the argument within the constructor name.
we are able to pass this key-word inside the constructor also. it is beneficial if we should use one item in more than one classes.
Example:
class my
{  
     db obj;  
     my(db obj)
    {  
         this.obj=obj;  
    }  
    void display()
    {  
    System.out.println(obj.data);
    }  
}  
  
class db
{  
     int data=77;  
     db()
    {  
        my m=new B(this);  
        m.display();  
    }  
    public static void main(String args[])
   {  
        db a=new db();  
   }  
}  
Output:
77

6. this will be used to return the modern class example from the approach.
we are able to go back this keyword as a statement from the technique. In such case, go back a form of the method ought to be the magnificence kind.
Example:
class my
{  
     my getmy()
     {  
           return this;  
     }  
     void bdp()
     {
           System.out.println("hi javapointtutorial");
     }  
}  
class demo
{  
     public static void main(String args[])
     {  
     new my().getmy().bdp();  
     }  
}  
Output:
 hi javapointtutorial



No comments:

Post a Comment

Top 200+ Core Java Interview Questions And Answers For 2019

Most Popular Java Interview Questions Given below is a comprehensive list of most important and commonly asked basic and advanced Java p...