More of a beginner's question. Please use the code tags to preserve your indentation.
What you are doing is enforcing a precondition. If you call the precondition P and the substitution S then you have P|S ie P is a precondition for S. That would correspond to writing "must not be null" in the documentation comments. But the user might still pass null and get into trouble afterwards, which is not your fault. Now you can be more assertive about your precondition and enforce it by upgrading it to a guard which we shall call G. You write G--->S pronounced G guards S. It means in fact that S will only run if G is true. You could write . . . if (anId == null) id = ("new ID"); That won't work well, so you have gone up one stage in assertiveness and thrown an Exception. You are really now enforcing the guard.
Yes, throwing an Exception like that is the correct thing to do.
You should add comments to the documentation and a @throws NullPointerException tag (in the documentation comment) so the user knows what to expect.
Joined: Mar 05, 2007
Thanks And i have some doubts in the below code:
1.can we pass null parameter in the super constructor? 2.serialNo is a final variable.can we assign getNextSerialNo() method to a final variable?is this valid?
Originally posted by Rashmi Dupati: Thanks And i have some doubts in the below code:
1.can we pass null parameter in the super constructor?
Theoretically? Yes. Actually? That depends on the contract the ConceptA's Constructor has. If it tells you that the parameter must be non-null, the call may succeed but you may get un-expected results or exceptions. If it doesn't tell you if the second parameter may be null or not then you should look through the code and see what the consequences of setting it to null will be... any NullPointerExceptions? Some other logical error? Or you could test it out and see what you get.
Originally posted by Rashmi Dupati: 2.serialNo is a final variable.can we assign getNextSerialNo() method to a final variable?is this valid?
Yes, you can assign to a final variable exactly once, and the assignment must be in an initializer or the constructor.
Joined: Oct 13, 2005
Agree with Steve Luke, and we are only too pleased when we help somebody.
But I think you ought to have started a new thread, since you are asking new questions. And please use the code tags and indent code at 4 spaces per level; it makes it much easier to read.