wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Coding doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Coding doubts" Watch "Coding doubts" New topic
Author

Coding doubts

Rashmi Dupati
Greenhorn

Joined: Mar 05, 2007
Posts: 22
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

Joined: Feb 25, 2006
Posts: 266
Err, what actually is your "doubt"?
[ October 08, 2008: Message edited by: Piet Verdriet ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
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

Joined: Mar 05, 2007
Posts: 22
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

Joined: Jan 28, 2003
Posts: 4165
    
  21

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.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
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.
 
 
subject: Coding doubts
 
Similar Threads
dynamic (?!) casting
Need to find the bugs in the code
OO Concepts
Rationale for Set not containing a get(Object 0) method?
How to align items inside the selectManyCheckbox ?