File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Invalid Parameter Passed To Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Invalid Parameter Passed To Constructor" Watch "Invalid Parameter Passed To Constructor" New topic

Invalid Parameter Passed To Constructor

Craig Oliver

Joined: Aug 25, 2003
Posts: 21
I am seeking advice on what should happen if constructor passed invalid data and unable to create new object. Is this responsibility of object class or calling class to manage this? This best illustrated with simple example below.
Say have simple class called DayOfWeek. It only has once instance variable that stores day of week as number (1=Sunday,..7=Saturday). It simply stores day of week and has various format methods to return day of week eg Tues, Tueday etc. There is constructor that accepts integer parameter of day of week (1-7).
If object instansiated with invalid constructor parameter (eg 8). Confused about what should happen to prevent invalid object being created.
Do not wish to create DayOfWeek object that contains invalid data.
Should constructor check if parameter is in valid range? If it is not, what should happen? Should it display message or how do you communicate failure back to creating module?
Should there be a static isValid method in DayOfWeek class that calling module calls first to check if day is valid before attempting to instansiate (get NEW) DayOfWeek object?
ie whose responsibility should this be? If pass in invalid parameter, just can't keep rolling on as if all okay.
I have searched for this, but had no success. If I have missed please advise.
Thanks in advance.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Should constructor check if parameter is in valid range? If it is not, what should happen? Should it display message or how do you communicate failure back to creating module?
Sounds to me like you overlooked the exception throwing and handling mechanism in Java. Take a look at Sun's
Handling Errors with Exceptions tutorial.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
There are a couple of options. One is to throw an Exception which would cause the instantiation to fail. The other option is to use a class to insure that only valid data can be passed to your constructor:

Change your constructor to only accept DayOfWeekUnit objects.
This would be a more advanced solution and really shouldn't be in the beginner's forum.
[ October 06, 2003: Message edited by: Thomas Paul ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Herb Schildt
Ranch Hand

Joined: Oct 01, 2003
Posts: 253
Craig: I want to re-emphasize what Eugene and Thomas are saying about the use of exceptions in the type of situation that you describe. In general, if an object cannot be created, its constructor should throw an exception that indicates the problem. It is then the exception handler's responsibility to take appropriate action, such as fixing or reporting the problem. The situation of a failed instantiation is one of the classic reasons that exceptions are part of Java. Their use is fundamental to successful Java programming.
To your question about whether a constructor should check that the data it is passed is valid. The answer is definitely Yes! No constructor should be allowed to construct invalid objects.
As to using a static "isValid" method to check before constructing the object, this might be a useful convenience, but it won't prevent the problem you describe. The reason is that it does not actually prevent someone from attempting to construct an invalid object anyway. That is, I can call isValid, but ignore its result, or I can just attempt to construct an object without validating the data first. Again, the constructor must determine if the data that it is passed is valid. If it isn't, the constructor should throw an exception.
Finally, Thomas' second option shows how you can head-off invalid data before an object is constructed. But as Thomas states, this is an advanced solution. You should explore it only after you fully understand the use of exceptions.

For my latest books on Java, including Introducing JavaFX 8 Programming, see
I agree. Here's the link:
subject: Invalid Parameter Passed To Constructor
It's not a secret anymore!