java4all@1986 java. Powered by Blogger.

Can we call one constructor from another Constructor?

>> Sunday, May 29, 2011

Yes ,We can call one constructor from another Constructor by using this.() method.

Look the below Example:



class  Pet
{
    int id=9;
    String type="check";
    public Pet(int id) {
this.id = id;           // “this” means this object
}
public Pet (int id, String type) {
this(id);                      // calls constructor public Pet(int id)
this.type = type;               // ”this” means this object
}
    public static void main(String[] args)
    {
        Pet p=new Pet(9,"check");
    }
}


0 comments:

Post a Comment

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP