• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Coding doubts

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Err, what actually is your "doubt"?
[ October 08, 2008: Message edited by: Piet Verdriet ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic