Thursday, 29 January 2015

this keyword in java

this is java keyword .this is reference variable that refer to current object.
1. this keyword is used to refer current class instance variable.
2. this() can be invoke current class constructor.
3.using this keyword invoke the current class method
     

1 comment:

  1. //

    public class ThisKeyword {


    int rno;
    String name;


    public ThisKeyword(int rno,String name) {
    this.rno=rno;
    this.name=name;

    }
    void arithmatic()
    {
    System.out.println(rno+""+name);
    }
    public static void main(String[] args) {
    ThisKeyword th=new ThisKeyword(111,"vikram");
    ThisKeyword th1=new ThisKeyword(112,"rahul");
    th.arithmatic();
    th1.arithmatic();

    }

    }

    *******************************

    package thisk;

    public class ThisMethod {


    void sum()
    {

    System.out.println(20+20);
    }

    void mul()
    {
    this.sum();
    System.out.println(20*5);

    }

    public static void main(String[] args) {
    ThisMethod th=new ThisMethod();
    //th.sum();
    th.mul();

    }

    }
    **********************************


    package thisk;

    public class CallConstru1 {


    int rno;
    String name,city;
    CallConstru1(int rno,String name)
    {
    this.rno=rno;
    this.name=name;
    System.out.println("Construstor one");
    }


    CallConstru1(int rno,String name,String city)
    {


    this(rno,name);
    this.city=city;

    System.out.println("Constructor Second...");

    }

    void display()
    {
    System.out.println(rno+""+name+""+city);


    }

    public static void main(String args[])
    {

    CallConstru1 c1=new CallConstru1(12,"satish");
    CallConstru1 c2=new CallConstru1(11,"vikram","kolhapur");
    c1.display();
    c2.display();


    }
    }








    ReplyDelete