aspose file tools*
The moose likes Beginning Java and the fly likes Assign new value to object ref in method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assign new value to object ref in method" Watch "Assign new value to object ref in method" New topic
Author

Assign new value to object ref in method

Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
I am assigning new value to my whereStatement in the appendToWhere method and the value of whereStatement is null when returning from appendToWhere,
Isn't this call ByRef?
I will be glad to get explenation.


The function body:



Thank you
[ July 29, 2008: Message edited by: Sharon whipple ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3682
    
  16
Don't you get a NullPointerException as soon as you call appendToWhere ? The first thing you do in there is dereference the whereStatement parameter which is null.

The reason this is not call by reference (which Java never uses) has been discussed many times. A quick search should give you the answer.
[ July 29, 2008: Message edited by: Joanne Neal ]

Joanne
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
I fixed the method so i don't get a NullPointerException

Is this case will be the same in C++ or C# ?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3682
    
  16
Originally posted by Sharon whipple:
Is this case will be the same in C++ or C# ?


I don't know about C#, but C++ allows call by reference, so it would be possible to do what you are trying to do, but it also allows you to write the method so it works the same way as Java does.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

In C++ it does the same as Java by default; you have to explicitly specify it is a reference (using & I believe).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Originally posted by Rob Prime:
In C++ it does the same as Java by default; you have to explicitly specify it is a reference (using & I believe).
I thought C and C++ were call-by-value too. But you can pass a pointer to a method, as you say, with the & operator.But I thought you are actually passing the value of the pointer, thereby mimicking pass-by-value. You can't alter the pointer, but you can alter its contents.

[edit]Last but one line should read "mimicking pass-by-reference."[/edit]
[ July 30, 2008: Message edited by: Campbell Ritchie ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Originally posted by Campbell Ritchie:
I thought C and C++ were call-by-value too. But you can pass a pointer to a method, as you say, with the & operator.But I thought you are actually passing the value of the pointer, thereby mimicking pass-by-value. You can't alter the pointer, but you can alter its contents.

[edit]Last but one line should read "mimicking pass-by-reference."[/edit]

[ July 30, 2008: Message edited by: Campbell Ritchie ]

That's not quite what I meant. That's not using a reference but a pointer. Something that does not exist in Java.

In C++ (not C!) you can do the following:

This will print "x = 10, y = 5". Remove one or both &s and there is no more swapping.
Note how you do not have to use any special syntax in the method call, just the method definition. A real improvement over the pointer syntax.
 
Don't get me started about those stupid light bulbs.
 
subject: Assign new value to object ref in method