3

I'm new to Java and I'm asking this question just to help me better understand OOP.

Let's say I'm defining a new Class called Hour. To instantiate this Class, we need to specify an integer to indicate the hour of this instance.

Hour hr = new Hour(16); // this means to define an hour that indicates 4pm.

So when we define the Hour Class here, the parameter for the constructor should within the range [0, 24). How can we define such a parameter and can I throw an error when a parameter that is out of this range is specified?

Thanks.

Thomas Edison
  • 63
  • 2
  • 8
  • 2
    Why would you re-invent the wheel, and not use the pre-defined Date API? Or even better use [Joda Time](http://www.joda.org/joda-time/) – Rohit Jain Sep 02 '13 at 18:32

3 Answers3

4

You can use IllegalArgumentException.

Thrown to indicate that a method has been passed an illegal or inappropriate argument.
example

public class Hour
{
    Hour(int hour)
    {
       if(hour>=24 || hour<0)
        {
           throw new IllegalArgumentException("Hour should in the range of [0-23].");
       }
   }
  ...............
}  
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
  • 2
    Make it `hour >= 24`. And it would be better to throw `InstantiationException`, with appropriate message, as that's inside a constructor. – Rohit Jain Sep 02 '13 at 18:34
  • @RohitJain yes we can throw `InstantiationException` also,but it is a checked exception we need to `throws` it. – Prabhaker A Sep 02 '13 at 18:49
  • @RohitJain hm... No problem in that.But I think am thinking unnecessary here.Thank you for your suggestion I will update the answer. – Prabhaker A Sep 02 '13 at 18:52
  • No leave it as it is. I think this is ok. – Rohit Jain Sep 02 '13 at 18:53
  • @RohitJain `InstantiationException` is wrong here, it should clearly be `IllegalArgumentException`, becaus this is what it is. The `InstantiationException` is used inthe context of reflection. Read the docs, please. – kap Feb 14 '21 at 22:19
1

If you would like the compiler to catch the error, you could define an enum for the hours, then use that as the parameter of the constructor of Hour. This might make the Hour class useless, though.

public class Hours {
    _1, _2, _3, // etc.
}

public class Hour {
    public Hour(Hours hour) { // no need for runtime check here, can not be wrong}
}

Hour hour = new Hour(Hours._3);

This technique may not be the best here, but generally it is better to rely on compile time checks than on runtime ones.

Katona
  • 4,816
  • 23
  • 27
1

Unfortunately Java unlike Pascal and other languages does not support range types. You can however use the other techniques.

The simplest way is to check the value in code.

class Hour {
    private final int h;
    public Hour(int h) {
        if (h < 0 || h >= 24) {
            throw new IllegalArgumentException("There are only 24 hours");
        }
        this.h = h;
    }
}

You can also use more sophisticated techniques. Take a look on java Validation API In this case your code will look like:

class Hour {
    @Max(23)
    @Min(0)
    private final int h;
    public Hour(int h) {
        this.h = h;
    }
}

But you will have to use one of available implementations of this API and invoke it.

AlexR
  • 114,158
  • 16
  • 130
  • 208