Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Coding doubts

 
Rashmi Dupati
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all

I have some doubts in coding.please answer my queries.below is a sort of code can we use the code like this.

protected Concept( String anId )
8 {
9 if ( anId == null )
10 {
11 throw new NullPointerException( "id must not be null" );
12 }
13
14 id = anId;
15 }

thanks in advance

Regards
Rashmi
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Err, what actually is your "doubt"?
[ October 08, 2008: Message edited by: Piet Verdriet ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rashmi Dupati
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?




Thanks in advance

Regards
Rashmi
[edit]Add code tags. CR[/edit]
[ October 09, 2008: Message edited by: Campbell Ritchie ]
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic