Construtors

Construtors

Following is the Program I wrote to use constructors for the first time. I would like to obtain o/p as 1 1 2000 but instead i am getting

O/P:

A
examples.Demo@cac268

Program:

package examples;

class Demo{
    public int day;
    public int month;
    public int year;

Demo(int day, int month, int year){
    System.out.println("A");
    setDate(day,month,year);
}

public void setDate(int day, int month, int year){
        this.day=day;
        this.month=month;
        this.year=year;
    }

public static void main(String args[]){

    Demo Ob1= new Demo(1,1,2000);
    System.out.println(Ob1);
}
}

Please tell me where am I using constructor concept wrongly...Thanks in advance.

View Answers

May 8, 2012 at 3:12 PM

class Demo{
    public int day;
    public int month;
    public int year;

Demo(int day, int month, int year){
    System.out.println("A");
    setDate(day,month,year);
}
public String toString() {
        return day + " " + month+" "+year;
    }

public void setDate(int day, int month, int year){
        this.day=day;
        this.month=month;
        this.year=year;
    }

public static void main(String args[]){

    Demo Ob1= new Demo(1,1,2000);
    System.out.println(Ob1);
}
}

May 8, 2012 at 4:32 PM

Thanks :)









Related Tutorials/Questions & Answers:
Construtors

Ads