• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assign new value to object ref in method

 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++ it does the same as Java by default; you have to explicitly specify it is a reference (using & I believe).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic