programming example for this keyword

//---------------------------------------------this keyword usage in the program---------------------------------

//program1

package java2;

public class Java2 {
    
    int age;
    String name;
    
    Java2()
    {
        System.out.println("hi");
        
    }
    Java2(int age,String name)
    {
        this.age=age;//to avoid the conflict between the variable names we need to use  this keyword.
                        //if we won't use this keyword the default value for the instance variable will be assigned.
        this.name=name;
        
    }
    
    void display()
    {
        System.out.println(age+" "+name);
    }

 
    public static void main(String[] args) {
       
        Java2 j=new Java2();
        Java2 j1=new Java2(10,"ravi");
        Java2 j2=new Java2(20,"prasad");
        j1.display();
        j2.display();
    
}
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//-----this keyword is used to call the current class constructor-----------------------

//program2

package java2;

public class Java2
{
     int no;
     String name,addr;
     
     Java2()
     {
         System.out.println("Welcome");
     }
     Java2(int no)
     {
         this();//while calling the constructor using this keyword,,this must be the first statement..
                //if this() is in the second statement or in other higher statement then we get the error
         this.no=no;
     }
     Java2(int no,String name)
     {
         this(no);
         this.name=name;
     }
     Java2(int no,String name,String addr)
     {
         this(no,name);
        //this();---------call for the constructor must be the first statement...
         this.addr=addr;
     }
    void display()
    {
        System.out.println(no+" "+name+" "+addr);
    }
    public static void main(String[] args)
    {
        Java2 j=new Java2(123,"ravi","harihar");
        j.display();
        
    }

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

//----------------------------using this in calling the current class method----------------------------------------------

package java2;

public class Java2
{
    void dvg()
    {
        System.out.println("Journy begins from Dvg");
    }
    void chitradurg()
    {
        dvg();
        System.out.println("had a cup of tea in chitradurga");
    }
    void tumkur()
    {
        this.chitradurg();//wee can explicitly mention this keyword as well.
        System.out.println("had snaks in tumkur");
    }
    void bangalore()
    {
        tumkur();//calling normal method.. //internally this.tumkur() will be called....
        this.chitradurg();//this keyword can be used for the second statement as well for calling the normal methods..
                        //but the constructor call must be done at the first statement only.
        System.out.println("successfully reached Bangalore");
    }
    public static void main(String []args)
    {
        Java2 j=new Java2();
        j.bangalore();
    }
}
 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//-------------using this keyword as reference to the current class...and printing the address---------------------------
package java2;

public class Java2
{
     void sample(Java2 a)
     {
         System.out.println(a);
     }
     void sample1()
     {
         sample(this);//this refers to the current class reference
         System.out.println(this);
     }
    public static void main(String [] args)
    {
        Java2 j=new Java2();
        j.sample1();
        System.out.println(j);
        
    }
}
 

Posted on by