I am just learning Java and am trying to use variables from objects that I passed into another method. Can someone help me here

package data;


public class Data {

    private int day, month, year;
    
    public Data(int day, int month, int year)
    {
        this.day = day;
        this.month = month;
        this.year = year;
    }
    
    public int getDay()
    {
        return day;
    }
    public int getMonth()
    {
        return month;
    }
    public int getYear()
    {
        return year;
    }
    void setDay(int day)
    {
        this.day = day;
    }
    void setMonth(int month)
    {
        this.month = month;
    }
    void setYear(int year)
    {
        this.year = year;
    }
}
    
    int compareTo (Date current, Date former)
{
    if(current.year < former.year)
    {
        return 0;
    }
    else
        if(current.month < former.month)
        {
            return 0;
        }
    else
            if(current.day < former.day)
            {
                return 0;
            }
    else
                if(current.year == former.year && current.month == former.month 
                        && current.day == former.day)
                {
                    return -1;
                }
    else
                {
                    return 1;
                }
    
                }

It gives an error on the line with the close bracket after the return statements and on the line 41. Everything else works fine.

Recommended Answers

All 5 Replies

} on line 39 matches the { on line 4 and closes the definition of the class. Then there are more methods - but you can't have a method outside a class.

What kind of error?

EDIT: Oops, hadn't seen JamesCherrill's post

@ JamesCherrill. Thanks. It was in the class before, but when inside the class, all the object variables eg. current.month give an error:
cannot find symbol
symbol: variable month
location: variable current of type data.Date.

"Date" != "Data" - check your method signatures vs class name

Wow. I can't believe I didn't notice that. It is working fine now. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.