Java Date class- switches and ifs in the constructor I'm a beginner i have an assignment to write a Date class (as part of a bigger project). in my question i focus on the constructor. here's some background: the given guidelines are that the date is not expected to be valid and the following instance variables expect this input range: day- integer 1-31 month- integer 1-12 year- integer 4 digits year.

now, if an invalid day/month/year or invalid date (such as 31.2.2010) is entered, the object will be created with the date of 1.1.2000.

this is the code I've come up with and it does compile and seem to work fine.

public class Date
{
    private int _day;
    private int _month;
    private int _year;


    public Date (int day, int month, int year)  
    {
        switch (month)
        {
            case 1:                
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12: if ((day&gt;0 &amp;&amp; day&lt;32) &amp;&amp; (year&gt;999 &amp;&amp; year&lt;10000))
            {
                _day=day;
                _month=month;
                _year=year;           
            }
            else
            {
                _day=1;
                _month=1;
                _year=2000;
            }
            break;

            case 4:
            case 6:
            case 9:
            case 11: if ((day&gt;0 &amp;&amp; day&lt;31) &amp;&amp; (year&gt;999 &amp;&amp; year&lt;10000))
            {
                _day=day;
                _month=month;
                _year=year;           
            }
            else
            {
                _day=1;
                _month=1;
                _year=2000;
            }
            break;

            case 2: if (leap(year))
            {
                if ((day&gt;0 &amp;&amp; day&lt;30) &amp;&amp; (year&gt;999 &amp;&amp; year&lt;10000))
                {
                    _day=day;
                    _month=month;
                    _year=year;           
                }
                else
                {
                    _day=1;
                    _month=1;
                    _year=2000;
                }
                break;
            }

            else
            {
                if ((day&gt;0 &amp;&amp; day&lt;29) &amp;&amp; (year&gt;999 &amp;&amp; year&lt;10000))
                {
                    _day=day;
                    _month=month;
                    _year=year;           
                }
                else
                {
                    _day=1;
                    _month=1;
                    _year=2000;
                }
                break;
            }
        }
    }

    /** check if leap year */
    private boolean leap (int y)  
    {
        return (y % 4 == 0 &amp;&amp; y % 100 != 0) || (y % 400 == 0);
    }
}


here are my questions:


Is it fine to put all that code in the constructor? will it greatly affect the processing time or cause an error? is there an alternative if its a problem?
Is any part of the code could be considered a bad practice? such as the switches and ifs? I'm not feeling to confident with this build despite it working fine...


sorry for indentation inconvenience and such, 1st post :(
Getting your class compiling and working is an achievement in itself. So don't let my comments discourage you, but see them as hints for the next learning steps.

Processing time

Don't worry about that until you really experience performance problems. Even expert programmers often fail when trying to predict the performance of their code. If you run into performance issues, then learn to use analysis tools like "profilers". But most probably, you won't need them for the next few years.

Code in constructor

It's good to do validity checks in constructors. But you're right, the constructor's readability suffers from its length. The key to readability is introducing good abstractions instead of writing lengthy code, as you already did with the leap() method. Writing a monthLength(int year, int month) method and using that in the constructor, you can get rid of most switch/if statements.

Invalid dates

You chose to create a 01.01.2000 date in case of invalid date elements. Professionals will instead throw an IllegalArgumentException.

As a Java beginner, it's quite likely that you haven't learnt about the concept of exceptions yet, but it's an important thing, an elegant way to tell someone who wants to construct e.g. a 31.2.2010 date that you can't give him that date as it doesn't exist. As a caller of a constructor or method, I always want to be informed about success or failure, and exceptions are a good way to do that.